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; |
Rafael J. Wysocki | f244d8b | 2013-12-31 13:39:42 +0100 | [diff] [blame] | 54 | acpi_handle other_handle; |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 55 | acpi_handle rom_handle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 56 | } nouveau_dsm_priv; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 57 | |
Dave Airlie | c839d74 | 2012-11-02 11:04:27 +1000 | [diff] [blame] | 58 | bool nouveau_is_optimus(void) { |
| 59 | return nouveau_dsm_priv.optimus_detected; |
| 60 | } |
| 61 | |
| 62 | bool nouveau_is_v1_dsm(void) { |
| 63 | return nouveau_dsm_priv.dsm_detected; |
| 64 | } |
| 65 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 66 | #define NOUVEAU_DSM_HAS_MUX 0x1 |
| 67 | #define NOUVEAU_DSM_HAS_OPT 0x2 |
| 68 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 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 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 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 | { |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 81 | int i; |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 82 | union acpi_object *obj; |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 83 | char args_buff[4]; |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 84 | union acpi_object argv4 = { |
| 85 | .buffer.type = ACPI_TYPE_BUFFER, |
| 86 | .buffer.length = 4, |
| 87 | .buffer.pointer = args_buff |
| 88 | }; |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 89 | |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 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; |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 93 | |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 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) { |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 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 | } |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 107 | ACPI_FREE(obj); |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 108 | } |
| 109 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 113 | static int nouveau_dsm(acpi_handle handle, int func, int arg) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 114 | { |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 115 | int ret = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 116 | union acpi_object *obj; |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 117 | union acpi_object argv4 = { |
| 118 | .integer.type = ACPI_TYPE_INTEGER, |
| 119 | .integer.value = arg, |
| 120 | }; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 121 | |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 122 | obj = acpi_evaluate_dsm_typed(handle, nouveau_dsm_muid, 0x00000102, |
| 123 | func, &argv4, ACPI_TYPE_INTEGER); |
| 124 | if (!obj) { |
| 125 | acpi_handle_info(handle, "failed to evaluate _DSM\n"); |
| 126 | return AE_ERROR; |
| 127 | } else { |
| 128 | if (obj->integer.value == 0x80000002) |
| 129 | ret = -ENODEV; |
| 130 | ACPI_FREE(obj); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 131 | } |
| 132 | |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 133 | return ret; |
Peter Lekensteyn | 9075e85 | 2011-12-17 12:53:43 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 136 | static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 137 | { |
Dave Airlie | 000703f | 2011-05-09 11:40:25 +1000 | [diff] [blame] | 138 | 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] | 139 | mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0); |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 140 | return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 141 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 142 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 143 | static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state) |
| 144 | { |
| 145 | int arg; |
| 146 | if (state == VGA_SWITCHEROO_ON) |
| 147 | arg = NOUVEAU_DSM_POWER_SPEED; |
| 148 | else |
| 149 | arg = NOUVEAU_DSM_POWER_STAMINA; |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 150 | nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 154 | static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 155 | { |
Dave Airlie | c839d74 | 2012-11-02 11:04:27 +1000 | [diff] [blame] | 156 | if (!nouveau_dsm_priv.dsm_detected) |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 157 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 158 | if (id == VGA_SWITCHEROO_IGD) |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 159 | 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] | 160 | else |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 161 | 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] | 162 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 163 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 164 | static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id, |
| 165 | enum vga_switcheroo_state state) |
| 166 | { |
| 167 | if (id == VGA_SWITCHEROO_IGD) |
| 168 | return 0; |
| 169 | |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 170 | /* Optimus laptops have the card already disabled in |
| 171 | * nouveau_switcheroo_set_state */ |
Dave Airlie | c839d74 | 2012-11-02 11:04:27 +1000 | [diff] [blame] | 172 | if (!nouveau_dsm_priv.dsm_detected) |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 173 | return 0; |
| 174 | |
Dave Airlie | fc5ea29 | 2010-05-31 17:10:52 +1000 | [diff] [blame] | 175 | return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 176 | } |
| 177 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 178 | static int nouveau_dsm_get_client_id(struct pci_dev *pdev) |
| 179 | { |
Dave Airlie | d1fbd92 | 2010-12-06 12:56:44 +1000 | [diff] [blame] | 180 | /* easy option one - intel vendor ID means Integrated */ |
| 181 | if (pdev->vendor == PCI_VENDOR_ID_INTEL) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 182 | return VGA_SWITCHEROO_IGD; |
Dave Airlie | d1fbd92 | 2010-12-06 12:56:44 +1000 | [diff] [blame] | 183 | |
| 184 | /* is this device on Bus 0? - this may need improving */ |
| 185 | if (pdev->bus->number == 0) |
| 186 | return VGA_SWITCHEROO_IGD; |
| 187 | |
| 188 | return VGA_SWITCHEROO_DIS; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static struct vga_switcheroo_handler nouveau_dsm_handler = { |
| 192 | .switchto = nouveau_dsm_switchto, |
| 193 | .power_state = nouveau_dsm_power_state, |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 194 | .get_client_id = nouveau_dsm_get_client_id, |
| 195 | }; |
| 196 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 197 | static int nouveau_dsm_pci_probe(struct pci_dev *pdev) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 198 | { |
Zhang Rui | 187b5b5 | 2013-09-03 08:32:00 +0800 | [diff] [blame] | 199 | acpi_handle dhandle; |
Peter Lekensteyn | 9075e85 | 2011-12-17 12:53:43 +0100 | [diff] [blame] | 200 | int retval = 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 201 | |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 202 | dhandle = ACPI_HANDLE(&pdev->dev); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 203 | if (!dhandle) |
| 204 | return false; |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 205 | |
Rafael J. Wysocki | f244d8b | 2013-12-31 13:39:42 +0100 | [diff] [blame] | 206 | if (!acpi_has_method(dhandle, "_DSM")) { |
| 207 | nouveau_dsm_priv.other_handle = dhandle; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 208 | return false; |
Rafael J. Wysocki | f244d8b | 2013-12-31 13:39:42 +0100 | [diff] [blame] | 209 | } |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 210 | if (acpi_check_dsm(dhandle, nouveau_dsm_muid, 0x00000102, |
| 211 | 1 << NOUVEAU_DSM_POWER)) |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 212 | retval |= NOUVEAU_DSM_HAS_MUX; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 213 | |
Jiang Liu | b072e53 | 2013-12-19 20:38:22 +0800 | [diff] [blame^] | 214 | if (acpi_check_dsm(dhandle, nouveau_op_dsm_muid, 0x00000100, |
| 215 | 1 << NOUVEAU_DSM_OPTIMUS_CAPS)) |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 216 | retval |= NOUVEAU_DSM_HAS_OPT; |
| 217 | |
Dave Airlie | 5addcf0 | 2012-09-10 14:20:51 +1000 | [diff] [blame] | 218 | if (retval & NOUVEAU_DSM_HAS_OPT) { |
| 219 | uint32_t result; |
| 220 | nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0, |
| 221 | &result); |
| 222 | dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n", |
| 223 | (result & OPTIMUS_ENABLED) ? "enabled" : "disabled", |
| 224 | (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "", |
| 225 | (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : ""); |
| 226 | } |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 227 | if (retval) |
| 228 | nouveau_dsm_priv.dhandle = dhandle; |
| 229 | |
| 230 | return retval; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 231 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 232 | |
| 233 | static bool nouveau_dsm_detect(void) |
| 234 | { |
| 235 | char acpi_method_name[255] = { 0 }; |
| 236 | struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; |
| 237 | struct pci_dev *pdev = NULL; |
| 238 | int has_dsm = 0; |
Dave Airlie | addde4e | 2012-05-02 20:26:24 +0100 | [diff] [blame] | 239 | int has_optimus = 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 240 | int vga_count = 0; |
Dave Airlie | 8116188 | 2010-12-06 12:57:45 +1000 | [diff] [blame] | 241 | bool guid_valid; |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 242 | int retval; |
| 243 | bool ret = false; |
Dave Airlie | 8116188 | 2010-12-06 12:57:45 +1000 | [diff] [blame] | 244 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 245 | /* lookup the MXM GUID */ |
Dave Airlie | 8116188 | 2010-12-06 12:57:45 +1000 | [diff] [blame] | 246 | guid_valid = mxm_wmi_supported(); |
Dave Airlie | 8116188 | 2010-12-06 12:57:45 +1000 | [diff] [blame] | 247 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 248 | if (guid_valid) |
| 249 | printk("MXM: GUID detected in BIOS\n"); |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 250 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 251 | /* now do DSM detection */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 252 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { |
| 253 | vga_count++; |
| 254 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 255 | retval = nouveau_dsm_pci_probe(pdev); |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 256 | if (retval & NOUVEAU_DSM_HAS_MUX) |
| 257 | has_dsm |= 1; |
| 258 | if (retval & NOUVEAU_DSM_HAS_OPT) |
| 259 | has_optimus = 1; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 260 | } |
| 261 | |
Emil Velikov | 4c60fac | 2013-10-09 08:25:16 +0100 | [diff] [blame] | 262 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) { |
| 263 | vga_count++; |
| 264 | |
| 265 | retval = nouveau_dsm_pci_probe(pdev); |
| 266 | if (retval & NOUVEAU_DSM_HAS_MUX) |
| 267 | has_dsm |= 1; |
| 268 | if (retval & NOUVEAU_DSM_HAS_OPT) |
| 269 | has_optimus = 1; |
| 270 | } |
| 271 | |
Dave Airlie | c839d74 | 2012-11-02 11:04:27 +1000 | [diff] [blame] | 272 | /* find the optimus DSM or the old v1 DSM */ |
| 273 | if (has_optimus == 1) { |
| 274 | acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, |
| 275 | &buffer); |
| 276 | printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n", |
| 277 | acpi_method_name); |
| 278 | nouveau_dsm_priv.optimus_detected = true; |
| 279 | ret = true; |
| 280 | } else if (vga_count == 2 && has_dsm && guid_valid) { |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 281 | acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, |
| 282 | &buffer); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 283 | printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n", |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 284 | acpi_method_name); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 285 | nouveau_dsm_priv.dsm_detected = true; |
Rafael J. Wysocki | f244d8b | 2013-12-31 13:39:42 +0100 | [diff] [blame] | 286 | /* |
| 287 | * On some systems hotplug events are generated for the device |
| 288 | * being switched off when _DSM is executed. They cause ACPI |
| 289 | * hotplug to trigger and attempt to remove the device from |
| 290 | * the system, which causes it to break down. Prevent that from |
| 291 | * happening by setting the no_hotplug flag for the involved |
| 292 | * ACPI device objects. |
| 293 | */ |
| 294 | acpi_bus_no_hotplug(nouveau_dsm_priv.dhandle); |
| 295 | acpi_bus_no_hotplug(nouveau_dsm_priv.other_handle); |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 296 | ret = true; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 297 | } |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 298 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 299 | |
| 300 | return ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void nouveau_register_dsm_handler(void) |
| 304 | { |
| 305 | bool r; |
| 306 | |
| 307 | r = nouveau_dsm_detect(); |
| 308 | if (!r) |
| 309 | return; |
| 310 | |
| 311 | vga_switcheroo_register_handler(&nouveau_dsm_handler); |
| 312 | } |
| 313 | |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 314 | /* Must be called for Optimus models before the card can be turned off */ |
| 315 | void nouveau_switcheroo_optimus_dsm(void) |
| 316 | { |
| 317 | u32 result = 0; |
| 318 | if (!nouveau_dsm_priv.optimus_detected) |
| 319 | return; |
| 320 | |
Dave Airlie | 5addcf0 | 2012-09-10 14:20:51 +1000 | [diff] [blame] | 321 | nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS, |
| 322 | 0x3, &result); |
| 323 | |
| 324 | nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, |
| 325 | NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result); |
| 326 | |
Peter Lekensteyn | d099230 | 2011-12-17 12:54:04 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 329 | void nouveau_unregister_dsm_handler(void) |
| 330 | { |
Andreas Heider | 2f3787a | 2012-05-21 00:14:50 +0100 | [diff] [blame] | 331 | if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected) |
| 332 | vga_switcheroo_unregister_handler(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 333 | } |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 334 | |
| 335 | /* retrieve the ROM in 4k blocks */ |
| 336 | static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios, |
| 337 | int offset, int len) |
| 338 | { |
| 339 | acpi_status status; |
| 340 | union acpi_object rom_arg_elements[2], *obj; |
| 341 | struct acpi_object_list rom_arg; |
| 342 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL}; |
| 343 | |
| 344 | rom_arg.count = 2; |
| 345 | rom_arg.pointer = &rom_arg_elements[0]; |
| 346 | |
| 347 | rom_arg_elements[0].type = ACPI_TYPE_INTEGER; |
| 348 | rom_arg_elements[0].integer.value = offset; |
| 349 | |
| 350 | rom_arg_elements[1].type = ACPI_TYPE_INTEGER; |
| 351 | rom_arg_elements[1].integer.value = len; |
| 352 | |
| 353 | status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer); |
| 354 | if (ACPI_FAILURE(status)) { |
| 355 | printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status)); |
| 356 | return -ENODEV; |
| 357 | } |
| 358 | obj = (union acpi_object *)buffer.pointer; |
| 359 | memcpy(bios+offset, obj->buffer.pointer, len); |
| 360 | kfree(buffer.pointer); |
| 361 | return len; |
| 362 | } |
| 363 | |
| 364 | bool nouveau_acpi_rom_supported(struct pci_dev *pdev) |
| 365 | { |
| 366 | acpi_status status; |
| 367 | acpi_handle dhandle, rom_handle; |
| 368 | |
Dave Airlie | f19467c | 2011-03-22 14:10:27 +1000 | [diff] [blame] | 369 | if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected) |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 370 | return false; |
| 371 | |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 372 | dhandle = ACPI_HANDLE(&pdev->dev); |
Dave Airlie | afeb3e1 | 2010-04-07 13:55:09 +1000 | [diff] [blame] | 373 | if (!dhandle) |
| 374 | return false; |
| 375 | |
| 376 | status = acpi_get_handle(dhandle, "_ROM", &rom_handle); |
| 377 | if (ACPI_FAILURE(status)) |
| 378 | return false; |
| 379 | |
| 380 | nouveau_dsm_priv.rom_handle = rom_handle; |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len) |
| 385 | { |
| 386 | return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len); |
| 387 | } |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 388 | |
Ben Skeggs | c007706 | 2012-07-26 08:51:21 +1000 | [diff] [blame] | 389 | void * |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 390 | nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector) |
| 391 | { |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 392 | struct acpi_device *acpidev; |
| 393 | acpi_handle handle; |
| 394 | int type, ret; |
| 395 | void *edid; |
| 396 | |
| 397 | switch (connector->connector_type) { |
| 398 | case DRM_MODE_CONNECTOR_LVDS: |
| 399 | case DRM_MODE_CONNECTOR_eDP: |
| 400 | type = ACPI_VIDEO_DISPLAY_LCD; |
| 401 | break; |
| 402 | default: |
Ben Skeggs | c007706 | 2012-07-26 08:51:21 +1000 | [diff] [blame] | 403 | return NULL; |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 404 | } |
| 405 | |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 406 | handle = ACPI_HANDLE(&dev->pdev->dev); |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 407 | if (!handle) |
Ben Skeggs | c007706 | 2012-07-26 08:51:21 +1000 | [diff] [blame] | 408 | return NULL; |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 409 | |
| 410 | ret = acpi_bus_get_device(handle, &acpidev); |
| 411 | if (ret) |
Ben Skeggs | c007706 | 2012-07-26 08:51:21 +1000 | [diff] [blame] | 412 | return NULL; |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 413 | |
| 414 | ret = acpi_video_get_edid(acpidev, type, -1, &edid); |
| 415 | if (ret < 0) |
Ben Skeggs | c007706 | 2012-07-26 08:51:21 +1000 | [diff] [blame] | 416 | return NULL; |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 417 | |
Ben Skeggs | c007706 | 2012-07-26 08:51:21 +1000 | [diff] [blame] | 418 | return kmemdup(edid, EDID_LENGTH, GFP_KERNEL); |
Ben Skeggs | a6ed76d | 2010-07-12 15:33:07 +1000 | [diff] [blame] | 419 | } |