1 /* $NetBSD: nouveau_acpi.c,v 1.2 2018/08/27 04:58:24 riastradh Exp $ */ 2 3 #include <sys/cdefs.h> 4 __KERNEL_RCSID(0, "$NetBSD: nouveau_acpi.c,v 1.2 2018/08/27 04:58:24 riastradh Exp $"); 5 6 #include <linux/pci.h> 7 #include <linux/acpi.h> 8 #include <linux/slab.h> 9 #include <linux/mxm-wmi.h> 10 #include <linux/vga_switcheroo.h> 11 #include <drm/drm_edid.h> 12 #include <acpi/video.h> 13 14 #include "nouveau_drm.h" 15 #include "nouveau_acpi.h" 16 17 #define NOUVEAU_DSM_LED 0x02 18 #define NOUVEAU_DSM_LED_STATE 0x00 19 #define NOUVEAU_DSM_LED_OFF 0x10 20 #define NOUVEAU_DSM_LED_STAMINA 0x11 21 #define NOUVEAU_DSM_LED_SPEED 0x12 22 23 #define NOUVEAU_DSM_POWER 0x03 24 #define NOUVEAU_DSM_POWER_STATE 0x00 25 #define NOUVEAU_DSM_POWER_SPEED 0x01 26 #define NOUVEAU_DSM_POWER_STAMINA 0x02 27 28 #define NOUVEAU_DSM_OPTIMUS_CAPS 0x1A 29 #define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B 30 31 #define NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 (3 << 24) 32 #define NOUVEAU_DSM_OPTIMUS_NO_POWERDOWN_PS3 (2 << 24) 33 #define NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED (1) 34 35 #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED) 36 37 /* result of the optimus caps function */ 38 #define OPTIMUS_ENABLED (1 << 0) 39 #define OPTIMUS_STATUS_MASK (3 << 3) 40 #define OPTIMUS_STATUS_OFF (0 << 3) 41 #define OPTIMUS_STATUS_ON_ENABLED (1 << 3) 42 #define OPTIMUS_STATUS_PWR_STABLE (3 << 3) 43 #define OPTIMUS_DISPLAY_HOTPLUG (1 << 6) 44 #define OPTIMUS_CAPS_MASK (7 << 24) 45 #define OPTIMUS_DYNAMIC_PWR_CAP (1 << 24) 46 47 #define OPTIMUS_AUDIO_CAPS_MASK (3 << 27) 48 #define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */ 49 50 static struct nouveau_dsm_priv { 51 bool dsm_detected; 52 bool optimus_detected; 53 acpi_handle dhandle; 54 acpi_handle rom_handle; 55 } nouveau_dsm_priv; 56 57 bool nouveau_is_optimus(void) { 58 return nouveau_dsm_priv.optimus_detected; 59 } 60 61 bool nouveau_is_v1_dsm(void) { 62 return nouveau_dsm_priv.dsm_detected; 63 } 64 65 #define NOUVEAU_DSM_HAS_MUX 0x1 66 #define NOUVEAU_DSM_HAS_OPT 0x2 67 68 #ifdef CONFIG_VGA_SWITCHEROO 69 static const char nouveau_dsm_muid[] = { 70 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D, 71 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4, 72 }; 73 74 static const char nouveau_op_dsm_muid[] = { 75 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47, 76 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0, 77 }; 78 79 static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result) 80 { 81 int i; 82 union acpi_object *obj; 83 char args_buff[4]; 84 union acpi_object argv4 = { 85 .buffer.type = ACPI_TYPE_BUFFER, 86 .buffer.length = 4, 87 .buffer.pointer = args_buff 88 }; 89 90 /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */ 91 for (i = 0; i < 4; i++) 92 args_buff[i] = (arg >> i * 8) & 0xFF; 93 94 *result = 0; 95 obj = acpi_evaluate_dsm_typed(handle, nouveau_op_dsm_muid, 0x00000100, 96 func, &argv4, ACPI_TYPE_BUFFER); 97 if (!obj) { 98 acpi_handle_info(handle, "failed to evaluate _DSM\n"); 99 return AE_ERROR; 100 } else { 101 if (obj->buffer.length == 4) { 102 *result |= obj->buffer.pointer[0]; 103 *result |= (obj->buffer.pointer[1] << 8); 104 *result |= (obj->buffer.pointer[2] << 16); 105 *result |= (obj->buffer.pointer[3] << 24); 106 } 107 ACPI_FREE(obj); 108 } 109 110 return 0; 111 } 112 113 /* 114 * On some platforms, _DSM(nouveau_op_dsm_muid, func0) has special 115 * requirements on the fourth parameter, so a private implementation 116 * instead of using acpi_check_dsm(). 117 */ 118 static int nouveau_check_optimus_dsm(acpi_handle handle) 119 { 120 int result; 121 122 /* 123 * Function 0 returns a Buffer containing available functions. 124 * The args parameter is ignored for function 0, so just put 0 in it 125 */ 126 if (nouveau_optimus_dsm(handle, 0, 0, &result)) 127 return 0; 128 129 /* 130 * ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. 131 * If the n-th bit is enabled, function n is supported 132 */ 133 return result & 1 && result & (1 << NOUVEAU_DSM_OPTIMUS_CAPS); 134 } 135 136 static int nouveau_dsm(acpi_handle handle, int func, int arg) 137 { 138 int ret = 0; 139 union acpi_object *obj; 140 union acpi_object argv4 = { 141 .integer.type = ACPI_TYPE_INTEGER, 142 .integer.value = arg, 143 }; 144 145 obj = acpi_evaluate_dsm_typed(handle, nouveau_dsm_muid, 0x00000102, 146 func, &argv4, ACPI_TYPE_INTEGER); 147 if (!obj) { 148 acpi_handle_info(handle, "failed to evaluate _DSM\n"); 149 return AE_ERROR; 150 } else { 151 if (obj->integer.value == 0x80000002) 152 ret = -ENODEV; 153 ACPI_FREE(obj); 154 } 155 156 return ret; 157 } 158 159 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id) 160 { 161 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0); 162 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0); 163 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id); 164 } 165 166 static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state) 167 { 168 int arg; 169 if (state == VGA_SWITCHEROO_ON) 170 arg = NOUVEAU_DSM_POWER_SPEED; 171 else 172 arg = NOUVEAU_DSM_POWER_STAMINA; 173 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg); 174 return 0; 175 } 176 177 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id) 178 { 179 if (!nouveau_dsm_priv.dsm_detected) 180 return 0; 181 if (id == VGA_SWITCHEROO_IGD) 182 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA); 183 else 184 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED); 185 } 186 187 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id, 188 enum vga_switcheroo_state state) 189 { 190 if (id == VGA_SWITCHEROO_IGD) 191 return 0; 192 193 /* Optimus laptops have the card already disabled in 194 * nouveau_switcheroo_set_state */ 195 if (!nouveau_dsm_priv.dsm_detected) 196 return 0; 197 198 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state); 199 } 200 201 static int nouveau_dsm_get_client_id(struct pci_dev *pdev) 202 { 203 /* easy option one - intel vendor ID means Integrated */ 204 if (pdev->vendor == PCI_VENDOR_ID_INTEL) 205 return VGA_SWITCHEROO_IGD; 206 207 /* is this device on Bus 0? - this may need improving */ 208 if (pdev->bus->number == 0) 209 return VGA_SWITCHEROO_IGD; 210 211 return VGA_SWITCHEROO_DIS; 212 } 213 214 static const struct vga_switcheroo_handler nouveau_dsm_handler = { 215 .switchto = nouveau_dsm_switchto, 216 .power_state = nouveau_dsm_power_state, 217 .get_client_id = nouveau_dsm_get_client_id, 218 }; 219 220 static int nouveau_dsm_pci_probe(struct pci_dev *pdev) 221 { 222 acpi_handle dhandle; 223 int retval = 0; 224 225 dhandle = ACPI_HANDLE(&pdev->dev); 226 if (!dhandle) 227 return false; 228 229 if (!acpi_has_method(dhandle, "_DSM")) 230 return false; 231 232 if (acpi_check_dsm(dhandle, nouveau_dsm_muid, 0x00000102, 233 1 << NOUVEAU_DSM_POWER)) 234 retval |= NOUVEAU_DSM_HAS_MUX; 235 236 if (nouveau_check_optimus_dsm(dhandle)) 237 retval |= NOUVEAU_DSM_HAS_OPT; 238 239 if (retval & NOUVEAU_DSM_HAS_OPT) { 240 uint32_t result; 241 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0, 242 &result); 243 dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n", 244 (result & OPTIMUS_ENABLED) ? "enabled" : "disabled", 245 (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "", 246 (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : ""); 247 } 248 if (retval) 249 nouveau_dsm_priv.dhandle = dhandle; 250 251 return retval; 252 } 253 254 static bool nouveau_dsm_detect(void) 255 { 256 char acpi_method_name[255] = { 0 }; 257 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; 258 struct pci_dev *pdev = NULL; 259 int has_dsm = 0; 260 int has_optimus = 0; 261 int vga_count = 0; 262 bool guid_valid; 263 int retval; 264 bool ret = false; 265 266 /* lookup the MXM GUID */ 267 guid_valid = mxm_wmi_supported(); 268 269 if (guid_valid) 270 printk("MXM: GUID detected in BIOS\n"); 271 272 /* now do DSM detection */ 273 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { 274 vga_count++; 275 276 retval = nouveau_dsm_pci_probe(pdev); 277 if (retval & NOUVEAU_DSM_HAS_MUX) 278 has_dsm |= 1; 279 if (retval & NOUVEAU_DSM_HAS_OPT) 280 has_optimus = 1; 281 } 282 283 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) { 284 vga_count++; 285 286 retval = nouveau_dsm_pci_probe(pdev); 287 if (retval & NOUVEAU_DSM_HAS_MUX) 288 has_dsm |= 1; 289 if (retval & NOUVEAU_DSM_HAS_OPT) 290 has_optimus = 1; 291 } 292 293 /* find the optimus DSM or the old v1 DSM */ 294 if (has_optimus == 1) { 295 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, 296 &buffer); 297 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n", 298 acpi_method_name); 299 nouveau_dsm_priv.optimus_detected = true; 300 ret = true; 301 } else if (vga_count == 2 && has_dsm && guid_valid) { 302 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, 303 &buffer); 304 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n", 305 acpi_method_name); 306 nouveau_dsm_priv.dsm_detected = true; 307 ret = true; 308 } 309 310 311 return ret; 312 } 313 314 void nouveau_register_dsm_handler(void) 315 { 316 bool r; 317 318 r = nouveau_dsm_detect(); 319 if (!r) 320 return; 321 322 vga_switcheroo_register_handler(&nouveau_dsm_handler); 323 } 324 325 /* Must be called for Optimus models before the card can be turned off */ 326 void nouveau_switcheroo_optimus_dsm(void) 327 { 328 u32 result = 0; 329 if (!nouveau_dsm_priv.optimus_detected) 330 return; 331 332 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS, 333 0x3, &result); 334 335 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 336 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result); 337 338 } 339 340 void nouveau_unregister_dsm_handler(void) 341 { 342 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected) 343 vga_switcheroo_unregister_handler(); 344 } 345 #else 346 void nouveau_register_dsm_handler(void) {} 347 void nouveau_unregister_dsm_handler(void) {} 348 void nouveau_switcheroo_optimus_dsm(void) {} 349 #endif 350 351 /* retrieve the ROM in 4k blocks */ 352 static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios, 353 int offset, int len) 354 { 355 acpi_status status; 356 union acpi_object rom_arg_elements[2], *obj; 357 struct acpi_object_list rom_arg; 358 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL}; 359 360 rom_arg.count = 2; 361 rom_arg.pointer = &rom_arg_elements[0]; 362 363 rom_arg_elements[0].type = ACPI_TYPE_INTEGER; 364 rom_arg_elements[0].integer.value = offset; 365 366 rom_arg_elements[1].type = ACPI_TYPE_INTEGER; 367 rom_arg_elements[1].integer.value = len; 368 369 status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer); 370 if (ACPI_FAILURE(status)) { 371 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status)); 372 return -ENODEV; 373 } 374 obj = (union acpi_object *)buffer.pointer; 375 len = min(len, (int)obj->buffer.length); 376 memcpy(bios+offset, obj->buffer.pointer, len); 377 kfree(buffer.pointer); 378 return len; 379 } 380 381 bool nouveau_acpi_rom_supported(struct device *dev) 382 { 383 acpi_status status; 384 acpi_handle dhandle, rom_handle; 385 386 dhandle = ACPI_HANDLE(dev); 387 if (!dhandle) 388 return false; 389 390 status = acpi_get_handle(dhandle, "_ROM", &rom_handle); 391 if (ACPI_FAILURE(status)) 392 return false; 393 394 nouveau_dsm_priv.rom_handle = rom_handle; 395 return true; 396 } 397 398 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) 399 { 400 return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len); 401 } 402 403 void * 404 nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector) 405 { 406 struct acpi_device *acpidev; 407 acpi_handle handle; 408 int type, ret; 409 void *edid; 410 411 switch (connector->connector_type) { 412 case DRM_MODE_CONNECTOR_LVDS: 413 case DRM_MODE_CONNECTOR_eDP: 414 type = ACPI_VIDEO_DISPLAY_LCD; 415 break; 416 default: 417 return NULL; 418 } 419 420 handle = ACPI_HANDLE(&dev->pdev->dev); 421 if (!handle) 422 return NULL; 423 424 ret = acpi_bus_get_device(handle, &acpidev); 425 if (ret) 426 return NULL; 427 428 ret = acpi_video_get_edid(acpidev, type, -1, &edid); 429 if (ret < 0) 430 return NULL; 431 432 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL); 433 } 434