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> |
| 6 | |
| 7 | #include "drmP.h" |
| 8 | #include "drm.h" |
| 9 | #include "drm_sarea.h" |
| 10 | #include "drm_crtc_helper.h" |
| 11 | #include "nouveau_drv.h" |
| 12 | #include "nouveau_drm.h" |
| 13 | #include "nv50_display.h" |
| 14 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 15 | #include <linux/vga_switcheroo.h> |
| 16 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 17 | #define NOUVEAU_DSM_SUPPORTED 0x00 |
| 18 | #define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00 |
| 19 | |
| 20 | #define NOUVEAU_DSM_ACTIVE 0x01 |
| 21 | #define NOUVEAU_DSM_ACTIVE_QUERY 0x00 |
| 22 | |
| 23 | #define NOUVEAU_DSM_LED 0x02 |
| 24 | #define NOUVEAU_DSM_LED_STATE 0x00 |
| 25 | #define NOUVEAU_DSM_LED_OFF 0x10 |
| 26 | #define NOUVEAU_DSM_LED_STAMINA 0x11 |
| 27 | #define NOUVEAU_DSM_LED_SPEED 0x12 |
| 28 | |
| 29 | #define NOUVEAU_DSM_POWER 0x03 |
| 30 | #define NOUVEAU_DSM_POWER_STATE 0x00 |
| 31 | #define NOUVEAU_DSM_POWER_SPEED 0x01 |
| 32 | #define NOUVEAU_DSM_POWER_STAMINA 0x02 |
| 33 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 34 | static struct nouveau_dsm_priv { |
| 35 | bool dsm_detected; |
| 36 | acpi_handle dhandle; |
| 37 | acpi_handle dsm_handle; |
| 38 | } nouveau_dsm_priv; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 39 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 40 | static const char nouveau_dsm_muid[] = { |
| 41 | 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D, |
| 42 | 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4, |
| 43 | }; |
| 44 | |
| 45 | static int nouveau_dsm(acpi_handle handle, int func, int arg, int *result) |
| 46 | { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 47 | struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 48 | struct acpi_object_list input; |
| 49 | union acpi_object params[4]; |
| 50 | union acpi_object *obj; |
| 51 | int err; |
| 52 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 53 | input.count = 4; |
| 54 | input.pointer = params; |
| 55 | params[0].type = ACPI_TYPE_BUFFER; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 56 | params[0].buffer.length = sizeof(nouveau_dsm_muid); |
| 57 | params[0].buffer.pointer = (char *)nouveau_dsm_muid; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 58 | params[1].type = ACPI_TYPE_INTEGER; |
| 59 | params[1].integer.value = 0x00000102; |
| 60 | params[2].type = ACPI_TYPE_INTEGER; |
| 61 | params[2].integer.value = func; |
| 62 | params[3].type = ACPI_TYPE_INTEGER; |
| 63 | params[3].integer.value = arg; |
| 64 | |
| 65 | err = acpi_evaluate_object(handle, "_DSM", &input, &output); |
| 66 | if (err) { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 67 | printk(KERN_INFO "failed to evaluate _DSM: %d\n", err); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 68 | return err; |
| 69 | } |
| 70 | |
| 71 | obj = (union acpi_object *)output.pointer; |
| 72 | |
| 73 | if (obj->type == ACPI_TYPE_INTEGER) |
| 74 | if (obj->integer.value == 0x80000002) |
| 75 | return -ENODEV; |
| 76 | |
| 77 | if (obj->type == ACPI_TYPE_BUFFER) { |
| 78 | if (obj->buffer.length == 4 && result) { |
| 79 | *result = 0; |
| 80 | *result |= obj->buffer.pointer[0]; |
| 81 | *result |= (obj->buffer.pointer[1] << 8); |
| 82 | *result |= (obj->buffer.pointer[2] << 16); |
| 83 | *result |= (obj->buffer.pointer[3] << 24); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | kfree(output.pointer); |
| 88 | return 0; |
| 89 | } |
| 90 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 91 | static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 92 | { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 93 | return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL); |
| 94 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 95 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 96 | static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state) |
| 97 | { |
| 98 | int arg; |
| 99 | if (state == VGA_SWITCHEROO_ON) |
| 100 | arg = NOUVEAU_DSM_POWER_SPEED; |
| 101 | else |
| 102 | arg = NOUVEAU_DSM_POWER_STAMINA; |
| 103 | nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 107 | static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 108 | { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 109 | if (id == VGA_SWITCHEROO_IGD) |
| 110 | return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_STAMINA); |
| 111 | else |
| 112 | return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_SPEED); |
| 113 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 114 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 115 | static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id, |
| 116 | enum vga_switcheroo_state state) |
| 117 | { |
| 118 | if (id == VGA_SWITCHEROO_IGD) |
| 119 | return 0; |
| 120 | |
| 121 | return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dsm_handle, state); |
| 122 | } |
| 123 | |
| 124 | static int nouveau_dsm_init(void) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int nouveau_dsm_get_client_id(struct pci_dev *pdev) |
| 130 | { |
| 131 | if (nouveau_dsm_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev)) |
| 132 | return VGA_SWITCHEROO_IGD; |
| 133 | else |
| 134 | return VGA_SWITCHEROO_DIS; |
| 135 | } |
| 136 | |
| 137 | static struct vga_switcheroo_handler nouveau_dsm_handler = { |
| 138 | .switchto = nouveau_dsm_switchto, |
| 139 | .power_state = nouveau_dsm_power_state, |
| 140 | .init = nouveau_dsm_init, |
| 141 | .get_client_id = nouveau_dsm_get_client_id, |
| 142 | }; |
| 143 | |
| 144 | static bool nouveau_dsm_pci_probe(struct pci_dev *pdev) |
| 145 | { |
| 146 | acpi_handle dhandle, nvidia_handle; |
| 147 | acpi_status status; |
| 148 | int ret; |
| 149 | uint32_t result; |
| 150 | |
| 151 | dhandle = DEVICE_ACPI_HANDLE(&pdev->dev); |
| 152 | if (!dhandle) |
| 153 | return false; |
| 154 | status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle); |
| 155 | if (ACPI_FAILURE(status)) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | ret= nouveau_dsm(nvidia_handle, NOUVEAU_DSM_SUPPORTED, |
| 160 | NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result); |
| 161 | if (ret < 0) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 162 | return false; |
| 163 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 164 | nouveau_dsm_priv.dhandle = dhandle; |
| 165 | nouveau_dsm_priv.dsm_handle = nvidia_handle; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 166 | return true; |
| 167 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 168 | |
| 169 | static bool nouveau_dsm_detect(void) |
| 170 | { |
| 171 | char acpi_method_name[255] = { 0 }; |
| 172 | struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; |
| 173 | struct pci_dev *pdev = NULL; |
| 174 | int has_dsm = 0; |
| 175 | int vga_count = 0; |
| 176 | while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { |
| 177 | vga_count++; |
| 178 | |
| 179 | has_dsm |= (nouveau_dsm_pci_probe(pdev) == true); |
| 180 | } |
| 181 | |
| 182 | if (vga_count == 2 && has_dsm) { |
| 183 | acpi_get_name(nouveau_dsm_priv.dsm_handle, ACPI_FULL_PATHNAME, &buffer); |
| 184 | printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n", |
| 185 | acpi_method_name); |
| 186 | nouveau_dsm_priv.dsm_detected = true; |
| 187 | return true; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | void nouveau_register_dsm_handler(void) |
| 193 | { |
| 194 | bool r; |
| 195 | |
| 196 | r = nouveau_dsm_detect(); |
| 197 | if (!r) |
| 198 | return; |
| 199 | |
| 200 | vga_switcheroo_register_handler(&nouveau_dsm_handler); |
| 201 | } |
| 202 | |
| 203 | void nouveau_unregister_dsm_handler(void) |
| 204 | { |
| 205 | vga_switcheroo_unregister_handler(); |
| 206 | } |