vga_switcheroo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright (c) 2010 Red Hat Inc.
  3. * Author : Dave Airlie <airlied@redhat.com>
  4. *
  5. *
  6. * Licensed under GPLv2
  7. *
  8. * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
  9. Switcher interface - methods require for ATPX and DCM
  10. - switchto - this throws the output MUX switch
  11. - discrete_set_power - sets the power state for the discrete card
  12. GPU driver interface
  13. - set_gpu_state - this should do the equiv of s/r for the card
  14. - this should *not* set the discrete power state
  15. - switch_check - check if the device is in a position to switch now
  16. */
  17. #include <linux/module.h>
  18. #include <linux/dmi.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/fs.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/fb.h>
  24. #include <linux/pci.h>
  25. #include <linux/vga_switcheroo.h>
  26. struct vga_switcheroo_client {
  27. struct pci_dev *pdev;
  28. struct fb_info *fb_info;
  29. int pwr_state;
  30. void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state);
  31. void (*reprobe)(struct pci_dev *pdev);
  32. bool (*can_switch)(struct pci_dev *pdev);
  33. int id;
  34. bool active;
  35. };
  36. static DEFINE_MUTEX(vgasr_mutex);
  37. struct vgasr_priv {
  38. bool active;
  39. bool delayed_switch_active;
  40. enum vga_switcheroo_client_id delayed_client_id;
  41. struct dentry *debugfs_root;
  42. struct dentry *switch_file;
  43. int registered_clients;
  44. struct vga_switcheroo_client clients[VGA_SWITCHEROO_MAX_CLIENTS];
  45. struct vga_switcheroo_handler *handler;
  46. };
  47. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
  48. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
  49. /* only one switcheroo per system */
  50. static struct vgasr_priv vgasr_priv;
  51. int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
  52. {
  53. mutex_lock(&vgasr_mutex);
  54. if (vgasr_priv.handler) {
  55. mutex_unlock(&vgasr_mutex);
  56. return -EINVAL;
  57. }
  58. vgasr_priv.handler = handler;
  59. mutex_unlock(&vgasr_mutex);
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(vga_switcheroo_register_handler);
  63. void vga_switcheroo_unregister_handler(void)
  64. {
  65. mutex_lock(&vgasr_mutex);
  66. vgasr_priv.handler = NULL;
  67. mutex_unlock(&vgasr_mutex);
  68. }
  69. EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
  70. static void vga_switcheroo_enable(void)
  71. {
  72. int i;
  73. int ret;
  74. /* call the handler to init */
  75. vgasr_priv.handler->init();
  76. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  77. ret = vgasr_priv.handler->get_client_id(vgasr_priv.clients[i].pdev);
  78. if (ret < 0)
  79. return;
  80. vgasr_priv.clients[i].id = ret;
  81. }
  82. vga_switcheroo_debugfs_init(&vgasr_priv);
  83. vgasr_priv.active = true;
  84. }
  85. int vga_switcheroo_register_client(struct pci_dev *pdev,
  86. void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
  87. void (*reprobe)(struct pci_dev *pdev),
  88. bool (*can_switch)(struct pci_dev *pdev))
  89. {
  90. int index;
  91. mutex_lock(&vgasr_mutex);
  92. /* don't do IGD vs DIS here */
  93. if (vgasr_priv.registered_clients & 1)
  94. index = 1;
  95. else
  96. index = 0;
  97. vgasr_priv.clients[index].pwr_state = VGA_SWITCHEROO_ON;
  98. vgasr_priv.clients[index].pdev = pdev;
  99. vgasr_priv.clients[index].set_gpu_state = set_gpu_state;
  100. vgasr_priv.clients[index].reprobe = reprobe;
  101. vgasr_priv.clients[index].can_switch = can_switch;
  102. vgasr_priv.clients[index].id = -1;
  103. if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  104. vgasr_priv.clients[index].active = true;
  105. vgasr_priv.registered_clients |= (1 << index);
  106. /* if we get two clients + handler */
  107. if (vgasr_priv.registered_clients == 0x3 && vgasr_priv.handler) {
  108. printk(KERN_INFO "vga_switcheroo: enabled\n");
  109. vga_switcheroo_enable();
  110. }
  111. mutex_unlock(&vgasr_mutex);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(vga_switcheroo_register_client);
  115. void vga_switcheroo_unregister_client(struct pci_dev *pdev)
  116. {
  117. int i;
  118. mutex_lock(&vgasr_mutex);
  119. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  120. if (vgasr_priv.clients[i].pdev == pdev) {
  121. vgasr_priv.registered_clients &= ~(1 << i);
  122. break;
  123. }
  124. }
  125. printk(KERN_INFO "vga_switcheroo: disabled\n");
  126. vga_switcheroo_debugfs_fini(&vgasr_priv);
  127. vgasr_priv.active = false;
  128. mutex_unlock(&vgasr_mutex);
  129. }
  130. EXPORT_SYMBOL(vga_switcheroo_unregister_client);
  131. void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
  132. struct fb_info *info)
  133. {
  134. int i;
  135. mutex_lock(&vgasr_mutex);
  136. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  137. if (vgasr_priv.clients[i].pdev == pdev) {
  138. vgasr_priv.clients[i].fb_info = info;
  139. break;
  140. }
  141. }
  142. mutex_unlock(&vgasr_mutex);
  143. }
  144. EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
  145. static int vga_switcheroo_show(struct seq_file *m, void *v)
  146. {
  147. int i;
  148. mutex_lock(&vgasr_mutex);
  149. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  150. seq_printf(m, "%d:%s:%c:%s:%s\n", i,
  151. vgasr_priv.clients[i].id == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
  152. vgasr_priv.clients[i].active ? '+' : ' ',
  153. vgasr_priv.clients[i].pwr_state ? "Pwr" : "Off",
  154. pci_name(vgasr_priv.clients[i].pdev));
  155. }
  156. mutex_unlock(&vgasr_mutex);
  157. return 0;
  158. }
  159. static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
  160. {
  161. return single_open(file, vga_switcheroo_show, NULL);
  162. }
  163. static int vga_switchon(struct vga_switcheroo_client *client)
  164. {
  165. if (vgasr_priv.handler->power_state)
  166. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
  167. /* call the driver callback to turn on device */
  168. client->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
  169. client->pwr_state = VGA_SWITCHEROO_ON;
  170. return 0;
  171. }
  172. static int vga_switchoff(struct vga_switcheroo_client *client)
  173. {
  174. /* call the driver callback to turn off device */
  175. client->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
  176. if (vgasr_priv.handler->power_state)
  177. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
  178. client->pwr_state = VGA_SWITCHEROO_OFF;
  179. return 0;
  180. }
  181. /* stage one happens before delay */
  182. static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
  183. {
  184. int i;
  185. struct vga_switcheroo_client *active = NULL;
  186. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  187. if (vgasr_priv.clients[i].active == true) {
  188. active = &vgasr_priv.clients[i];
  189. break;
  190. }
  191. }
  192. if (!active)
  193. return 0;
  194. if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
  195. vga_switchon(new_client);
  196. /* swap shadow resource to denote boot VGA device has changed so X starts on new device */
  197. active->pdev->resource[PCI_ROM_RESOURCE].flags &= ~IORESOURCE_ROM_SHADOW;
  198. new_client->pdev->resource[PCI_ROM_RESOURCE].flags |= IORESOURCE_ROM_SHADOW;
  199. return 0;
  200. }
  201. /* post delay */
  202. static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
  203. {
  204. int ret;
  205. int i;
  206. struct vga_switcheroo_client *active = NULL;
  207. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  208. if (vgasr_priv.clients[i].active == true) {
  209. active = &vgasr_priv.clients[i];
  210. break;
  211. }
  212. }
  213. if (!active)
  214. return 0;
  215. active->active = false;
  216. if (new_client->fb_info) {
  217. struct fb_event event;
  218. event.info = new_client->fb_info;
  219. fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
  220. }
  221. ret = vgasr_priv.handler->switchto(new_client->id);
  222. if (ret)
  223. return ret;
  224. if (new_client->reprobe)
  225. new_client->reprobe(new_client->pdev);
  226. if (active->pwr_state == VGA_SWITCHEROO_ON)
  227. vga_switchoff(active);
  228. new_client->active = true;
  229. return 0;
  230. }
  231. static ssize_t
  232. vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
  233. size_t cnt, loff_t *ppos)
  234. {
  235. char usercmd[64];
  236. const char *pdev_name;
  237. int i, ret;
  238. bool delay = false, can_switch;
  239. bool just_mux = false;
  240. int client_id = -1;
  241. struct vga_switcheroo_client *client = NULL;
  242. if (cnt > 63)
  243. cnt = 63;
  244. if (copy_from_user(usercmd, ubuf, cnt))
  245. return -EFAULT;
  246. mutex_lock(&vgasr_mutex);
  247. if (!vgasr_priv.active) {
  248. cnt = -EINVAL;
  249. goto out;
  250. }
  251. /* pwr off the device not in use */
  252. if (strncmp(usercmd, "OFF", 3) == 0) {
  253. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  254. if (vgasr_priv.clients[i].active)
  255. continue;
  256. if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_ON)
  257. vga_switchoff(&vgasr_priv.clients[i]);
  258. }
  259. goto out;
  260. }
  261. /* pwr on the device not in use */
  262. if (strncmp(usercmd, "ON", 2) == 0) {
  263. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  264. if (vgasr_priv.clients[i].active)
  265. continue;
  266. if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_OFF)
  267. vga_switchon(&vgasr_priv.clients[i]);
  268. }
  269. goto out;
  270. }
  271. /* request a delayed switch - test can we switch now */
  272. if (strncmp(usercmd, "DIGD", 4) == 0) {
  273. client_id = VGA_SWITCHEROO_IGD;
  274. delay = true;
  275. }
  276. if (strncmp(usercmd, "DDIS", 4) == 0) {
  277. client_id = VGA_SWITCHEROO_DIS;
  278. delay = true;
  279. }
  280. if (strncmp(usercmd, "IGD", 3) == 0)
  281. client_id = VGA_SWITCHEROO_IGD;
  282. if (strncmp(usercmd, "DIS", 3) == 0)
  283. client_id = VGA_SWITCHEROO_DIS;
  284. if (strncmp(usercmd, "MIGD", 4) == 0) {
  285. just_mux = true;
  286. client_id = VGA_SWITCHEROO_IGD;
  287. }
  288. if (strncmp(usercmd, "MDIS", 4) == 0) {
  289. just_mux = true;
  290. client_id = VGA_SWITCHEROO_DIS;
  291. }
  292. if (client_id == -1)
  293. goto out;
  294. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  295. if (vgasr_priv.clients[i].id == client_id) {
  296. client = &vgasr_priv.clients[i];
  297. break;
  298. }
  299. }
  300. vgasr_priv.delayed_switch_active = false;
  301. if (just_mux) {
  302. ret = vgasr_priv.handler->switchto(client_id);
  303. goto out;
  304. }
  305. if (client->active == true)
  306. goto out;
  307. /* okay we want a switch - test if devices are willing to switch */
  308. can_switch = true;
  309. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  310. can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
  311. if (can_switch == false) {
  312. printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
  313. break;
  314. }
  315. }
  316. if (can_switch == false && delay == false)
  317. goto out;
  318. if (can_switch == true) {
  319. pdev_name = pci_name(client->pdev);
  320. ret = vga_switchto_stage1(client);
  321. if (ret)
  322. printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
  323. ret = vga_switchto_stage2(client);
  324. if (ret)
  325. printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
  326. } else {
  327. printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
  328. vgasr_priv.delayed_switch_active = true;
  329. vgasr_priv.delayed_client_id = client_id;
  330. ret = vga_switchto_stage1(client);
  331. if (ret)
  332. printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
  333. }
  334. out:
  335. mutex_unlock(&vgasr_mutex);
  336. return cnt;
  337. }
  338. static const struct file_operations vga_switcheroo_debugfs_fops = {
  339. .owner = THIS_MODULE,
  340. .open = vga_switcheroo_debugfs_open,
  341. .write = vga_switcheroo_debugfs_write,
  342. .read = seq_read,
  343. .llseek = seq_lseek,
  344. .release = single_release,
  345. };
  346. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
  347. {
  348. if (priv->switch_file) {
  349. debugfs_remove(priv->switch_file);
  350. priv->switch_file = NULL;
  351. }
  352. if (priv->debugfs_root) {
  353. debugfs_remove(priv->debugfs_root);
  354. priv->debugfs_root = NULL;
  355. }
  356. }
  357. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
  358. {
  359. /* already initialised */
  360. if (priv->debugfs_root)
  361. return 0;
  362. priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
  363. if (!priv->debugfs_root) {
  364. printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
  365. goto fail;
  366. }
  367. priv->switch_file = debugfs_create_file("switch", 0644,
  368. priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
  369. if (!priv->switch_file) {
  370. printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
  371. goto fail;
  372. }
  373. return 0;
  374. fail:
  375. vga_switcheroo_debugfs_fini(priv);
  376. return -1;
  377. }
  378. int vga_switcheroo_process_delayed_switch(void)
  379. {
  380. struct vga_switcheroo_client *client = NULL;
  381. const char *pdev_name;
  382. bool can_switch = true;
  383. int i;
  384. int ret;
  385. int err = -EINVAL;
  386. mutex_lock(&vgasr_mutex);
  387. if (!vgasr_priv.delayed_switch_active)
  388. goto err;
  389. printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
  390. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  391. if (vgasr_priv.clients[i].id == vgasr_priv.delayed_client_id)
  392. client = &vgasr_priv.clients[i];
  393. can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
  394. if (can_switch == false) {
  395. printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
  396. break;
  397. }
  398. }
  399. if (can_switch == false || client == NULL)
  400. goto err;
  401. pdev_name = pci_name(client->pdev);
  402. ret = vga_switchto_stage2(client);
  403. if (ret)
  404. printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
  405. vgasr_priv.delayed_switch_active = false;
  406. err = 0;
  407. err:
  408. mutex_unlock(&vgasr_mutex);
  409. return err;
  410. }
  411. EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);