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 | |
| 10 | #include "drmP.h" |
| 11 | #include "drm.h" |
| 12 | #include "drm_sarea.h" |
| 13 | #include "drm_crtc_helper.h" |
| 14 | #include "nouveau_drv.h" |
| 15 | #include "nouveau_drm.h" |
| 16 | #include "nv50_display.h" |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 17 | #include "nouveau_connector.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 18 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 19 | #include <linux/vga_switcheroo.h> |
| 20 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 21 | #define NOUVEAU_DSM_SUPPORTED 0x00 |
| 22 | #define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00 |
| 23 | |
| 24 | #define NOUVEAU_DSM_ACTIVE 0x01 |
| 25 | #define NOUVEAU_DSM_ACTIVE_QUERY 0x00 |
| 26 | |
| 27 | #define NOUVEAU_DSM_LED 0x02 |
| 28 | #define NOUVEAU_DSM_LED_STATE 0x00 |
| 29 | #define NOUVEAU_DSM_LED_OFF 0x10 |
| 30 | #define NOUVEAU_DSM_LED_STAMINA 0x11 |
| 31 | #define NOUVEAU_DSM_LED_SPEED 0x12 |
| 32 | |
| 33 | #define NOUVEAU_DSM_POWER 0x03 |
| 34 | #define NOUVEAU_DSM_POWER_STATE 0x00 |
| 35 | #define NOUVEAU_DSM_POWER_SPEED 0x01 |
| 36 | #define NOUVEAU_DSM_POWER_STAMINA 0x02 |
| 37 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 38 | static struct nouveau_dsm_priv { |
| 39 | bool dsm_detected; |
| 40 | acpi_handle dhandle; |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 41 | acpi_handle rom_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 42 | } nouveau_dsm_priv; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 43 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 44 | static const char nouveau_dsm_muid[] = { |
| 45 | 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D, |
| 46 | 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4, |
| 47 | }; |
| 48 | |
Francisco Jerez | 6e86e04 | 2010-07-03 18:36:39 +0200 | [diff] [blame] | 49 | 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] | 50 | { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 51 | struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 52 | struct acpi_object_list input; |
| 53 | union acpi_object params[4]; |
| 54 | union acpi_object *obj; |
| 55 | int err; |
| 56 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 57 | input.count = 4; |
| 58 | input.pointer = params; |
| 59 | params[0].type = ACPI_TYPE_BUFFER; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 60 | params[0].buffer.length = sizeof(nouveau_dsm_muid); |
| 61 | params[0].buffer.pointer = (char *)nouveau_dsm_muid; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 62 | params[1].type = ACPI_TYPE_INTEGER; |
| 63 | params[1].integer.value = 0x00000102; |
| 64 | params[2].type = ACPI_TYPE_INTEGER; |
| 65 | params[2].integer.value = func; |
| 66 | params[3].type = ACPI_TYPE_INTEGER; |
| 67 | params[3].integer.value = arg; |
| 68 | |
| 69 | err = acpi_evaluate_object(handle, "_DSM", &input, &output); |
| 70 | if (err) { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 71 | printk(KERN_INFO "failed to evaluate _DSM: %d\n", err); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 72 | return err; |
| 73 | } |
| 74 | |
| 75 | obj = (union acpi_object *)output.pointer; |
| 76 | |
| 77 | if (obj->type == ACPI_TYPE_INTEGER) |
| 78 | if (obj->integer.value == 0x80000002) |
| 79 | return -ENODEV; |
| 80 | |
| 81 | if (obj->type == ACPI_TYPE_BUFFER) { |
| 82 | if (obj->buffer.length == 4 && result) { |
| 83 | *result = 0; |
| 84 | *result |= obj->buffer.pointer[0]; |
| 85 | *result |= (obj->buffer.pointer[1] << 8); |
| 86 | *result |= (obj->buffer.pointer[2] << 16); |
| 87 | *result |= (obj->buffer.pointer[3] << 24); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | kfree(output.pointer); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 95 | static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 96 | { |
Dave Airlie | 8116188 | 2010-12-06 12:57:45 +1000 | [diff] [blame^] | 97 | 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] | 98 | return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL); |
| 99 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 100 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 101 | static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state) |
| 102 | { |
| 103 | int arg; |
| 104 | if (state == VGA_SWITCHEROO_ON) |
| 105 | arg = NOUVEAU_DSM_POWER_SPEED; |
| 106 | else |
| 107 | arg = NOUVEAU_DSM_POWER_STAMINA; |
| 108 | nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 112 | static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 113 | { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 114 | if (id == VGA_SWITCHEROO_IGD) |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 115 | 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] | 116 | else |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 117 | 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] | 118 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 119 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 120 | static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id, |
| 121 | enum vga_switcheroo_state state) |
| 122 | { |
| 123 | if (id == VGA_SWITCHEROO_IGD) |
| 124 | return 0; |
| 125 | |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 126 | return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static int nouveau_dsm_init(void) |
| 130 | { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int nouveau_dsm_get_client_id(struct pci_dev *pdev) |
| 135 | { |
Dave Airlie | d1fbd92 | 2010-12-06 12:56:44 +1000 | [diff] [blame] | 136 | /* easy option one - intel vendor ID means Integrated */ |
| 137 | if (pdev->vendor == PCI_VENDOR_ID_INTEL) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 138 | return VGA_SWITCHEROO_IGD; |
Dave Airlie | d1fbd92 | 2010-12-06 12:56:44 +1000 | [diff] [blame] | 139 | |
| 140 | /* is this device on Bus 0? - this may need improving */ |
| 141 | if (pdev->bus->number == 0) |
| 142 | return VGA_SWITCHEROO_IGD; |
| 143 | |
| 144 | return VGA_SWITCHEROO_DIS; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static struct vga_switcheroo_handler nouveau_dsm_handler = { |
| 148 | .switchto = nouveau_dsm_switchto, |
| 149 | .power_state = nouveau_dsm_power_state, |
| 150 | .init = nouveau_dsm_init, |
| 151 | .get_client_id = nouveau_dsm_get_client_id, |
| 152 | }; |
| 153 | |
| 154 | static bool nouveau_dsm_pci_probe(struct pci_dev *pdev) |
| 155 | { |
| 156 | acpi_handle dhandle, nvidia_handle; |
| 157 | acpi_status status; |
| 158 | int ret; |
| 159 | uint32_t result; |
| 160 | |
| 161 | dhandle = DEVICE_ACPI_HANDLE(&pdev->dev); |
| 162 | if (!dhandle) |
| 163 | return false; |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 164 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 165 | status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle); |
| 166 | if (ACPI_FAILURE(status)) { |
| 167 | return false; |
| 168 | } |
| 169 | |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 170 | ret = nouveau_dsm(dhandle, NOUVEAU_DSM_SUPPORTED, |
| 171 | NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 172 | if (ret < 0) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 173 | return false; |
| 174 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 175 | nouveau_dsm_priv.dhandle = dhandle; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 176 | return true; |
| 177 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 178 | |
| 179 | static bool nouveau_dsm_detect(void) |
| 180 | { |
| 181 | char acpi_method_name[255] = { 0 }; |
| 182 | struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; |
| 183 | struct pci_dev *pdev = NULL; |
| 184 | int has_dsm = 0; |
| 185 | int vga_count = 0; |
Dave Airlie | 8116188 | 2010-12-06 12:57:45 +1000 | [diff] [blame^] | 186 | bool guid_valid; |
| 187 | |
| 188 | /* lookup the GUID */ |
| 189 | guid_valid = mxm_wmi_supported(); |
| 190 | if (!guid_valid) |
| 191 | return false; |
| 192 | |
| 193 | printk("MXM GUID detected in BIOS\n"); |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 194 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 195 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { |
| 196 | vga_count++; |
| 197 | |
| 198 | has_dsm |= (nouveau_dsm_pci_probe(pdev) == true); |
| 199 | } |
| 200 | |
| 201 | if (vga_count == 2 && has_dsm) { |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 202 | acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, &buffer); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 203 | printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n", |
| 204 | acpi_method_name); |
| 205 | nouveau_dsm_priv.dsm_detected = true; |
| 206 | return true; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | void nouveau_register_dsm_handler(void) |
| 212 | { |
| 213 | bool r; |
| 214 | |
| 215 | r = nouveau_dsm_detect(); |
| 216 | if (!r) |
| 217 | return; |
| 218 | |
| 219 | vga_switcheroo_register_handler(&nouveau_dsm_handler); |
| 220 | } |
| 221 | |
| 222 | void nouveau_unregister_dsm_handler(void) |
| 223 | { |
| 224 | vga_switcheroo_unregister_handler(); |
| 225 | } |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 226 | |
| 227 | /* retrieve the ROM in 4k blocks */ |
| 228 | static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios, |
| 229 | int offset, int len) |
| 230 | { |
| 231 | acpi_status status; |
| 232 | union acpi_object rom_arg_elements[2], *obj; |
| 233 | struct acpi_object_list rom_arg; |
| 234 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL}; |
| 235 | |
| 236 | rom_arg.count = 2; |
| 237 | rom_arg.pointer = &rom_arg_elements[0]; |
| 238 | |
| 239 | rom_arg_elements[0].type = ACPI_TYPE_INTEGER; |
| 240 | rom_arg_elements[0].integer.value = offset; |
| 241 | |
| 242 | rom_arg_elements[1].type = ACPI_TYPE_INTEGER; |
| 243 | rom_arg_elements[1].integer.value = len; |
| 244 | |
| 245 | status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer); |
| 246 | if (ACPI_FAILURE(status)) { |
| 247 | printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status)); |
| 248 | return -ENODEV; |
| 249 | } |
| 250 | obj = (union acpi_object *)buffer.pointer; |
| 251 | memcpy(bios+offset, obj->buffer.pointer, len); |
| 252 | kfree(buffer.pointer); |
| 253 | return len; |
| 254 | } |
| 255 | |
| 256 | bool nouveau_acpi_rom_supported(struct pci_dev *pdev) |
| 257 | { |
| 258 | acpi_status status; |
| 259 | acpi_handle dhandle, rom_handle; |
| 260 | |
| 261 | if (!nouveau_dsm_priv.dsm_detected) |
| 262 | return false; |
| 263 | |
| 264 | dhandle = DEVICE_ACPI_HANDLE(&pdev->dev); |
| 265 | if (!dhandle) |
| 266 | return false; |
| 267 | |
| 268 | status = acpi_get_handle(dhandle, "_ROM", &rom_handle); |
| 269 | if (ACPI_FAILURE(status)) |
| 270 | return false; |
| 271 | |
| 272 | nouveau_dsm_priv.rom_handle = rom_handle; |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) |
| 277 | { |
| 278 | return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len); |
| 279 | } |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 280 | |
| 281 | int |
| 282 | nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector) |
| 283 | { |
| 284 | struct nouveau_connector *nv_connector = nouveau_connector(connector); |
| 285 | struct acpi_device *acpidev; |
| 286 | acpi_handle handle; |
| 287 | int type, ret; |
| 288 | void *edid; |
| 289 | |
| 290 | switch (connector->connector_type) { |
| 291 | case DRM_MODE_CONNECTOR_LVDS: |
| 292 | case DRM_MODE_CONNECTOR_eDP: |
| 293 | type = ACPI_VIDEO_DISPLAY_LCD; |
| 294 | break; |
| 295 | default: |
| 296 | return -EINVAL; |
| 297 | } |
| 298 | |
| 299 | handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev); |
| 300 | if (!handle) |
| 301 | return -ENODEV; |
| 302 | |
| 303 | ret = acpi_bus_get_device(handle, &acpidev); |
| 304 | if (ret) |
| 305 | return -ENODEV; |
| 306 | |
| 307 | ret = acpi_video_get_edid(acpidev, type, -1, &edid); |
| 308 | if (ret < 0) |
| 309 | return ret; |
| 310 | |
Ben Skeggs | 24b102d | 2010-09-10 15:33:11 +1000 | [diff] [blame] | 311 | nv_connector->edid = kmemdup(edid, EDID_LENGTH, GFP_KERNEL); |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 312 | return 0; |
| 313 | } |