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