vga_switcheroo.c 12 KB

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