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