vga_switcheroo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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/console.h>
  26. #include <linux/vga_switcheroo.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->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
  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. /* swap shadow resource to denote boot VGA device has changed so X starts on new device */
  198. active->pdev->resource[PCI_ROM_RESOURCE].flags &= ~IORESOURCE_ROM_SHADOW;
  199. new_client->pdev->resource[PCI_ROM_RESOURCE].flags |= IORESOURCE_ROM_SHADOW;
  200. return 0;
  201. }
  202. /* post delay */
  203. static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
  204. {
  205. int ret;
  206. int i;
  207. struct vga_switcheroo_client *active = NULL;
  208. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  209. if (vgasr_priv.clients[i].active == true) {
  210. active = &vgasr_priv.clients[i];
  211. break;
  212. }
  213. }
  214. if (!active)
  215. return 0;
  216. active->active = false;
  217. if (new_client->fb_info) {
  218. struct fb_event event;
  219. console_lock();
  220. event.info = new_client->fb_info;
  221. fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
  222. console_unlock();
  223. }
  224. ret = vgasr_priv.handler->switchto(new_client->id);
  225. if (ret)
  226. return ret;
  227. if (new_client->reprobe)
  228. new_client->reprobe(new_client->pdev);
  229. if (active->pwr_state == VGA_SWITCHEROO_ON)
  230. vga_switchoff(active);
  231. new_client->active = true;
  232. return 0;
  233. }
  234. static ssize_t
  235. vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
  236. size_t cnt, loff_t *ppos)
  237. {
  238. char usercmd[64];
  239. const char *pdev_name;
  240. int i, ret;
  241. bool delay = false, can_switch;
  242. bool just_mux = false;
  243. int client_id = -1;
  244. struct vga_switcheroo_client *client = NULL;
  245. if (cnt > 63)
  246. cnt = 63;
  247. if (copy_from_user(usercmd, ubuf, cnt))
  248. return -EFAULT;
  249. mutex_lock(&vgasr_mutex);
  250. if (!vgasr_priv.active) {
  251. cnt = -EINVAL;
  252. goto out;
  253. }
  254. /* pwr off the device not in use */
  255. if (strncmp(usercmd, "OFF", 3) == 0) {
  256. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  257. if (vgasr_priv.clients[i].active)
  258. continue;
  259. if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_ON)
  260. vga_switchoff(&vgasr_priv.clients[i]);
  261. }
  262. goto out;
  263. }
  264. /* pwr on the device not in use */
  265. if (strncmp(usercmd, "ON", 2) == 0) {
  266. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  267. if (vgasr_priv.clients[i].active)
  268. continue;
  269. if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_OFF)
  270. vga_switchon(&vgasr_priv.clients[i]);
  271. }
  272. goto out;
  273. }
  274. /* request a delayed switch - test can we switch now */
  275. if (strncmp(usercmd, "DIGD", 4) == 0) {
  276. client_id = VGA_SWITCHEROO_IGD;
  277. delay = true;
  278. }
  279. if (strncmp(usercmd, "DDIS", 4) == 0) {
  280. client_id = VGA_SWITCHEROO_DIS;
  281. delay = true;
  282. }
  283. if (strncmp(usercmd, "IGD", 3) == 0)
  284. client_id = VGA_SWITCHEROO_IGD;
  285. if (strncmp(usercmd, "DIS", 3) == 0)
  286. client_id = VGA_SWITCHEROO_DIS;
  287. if (strncmp(usercmd, "MIGD", 4) == 0) {
  288. just_mux = true;
  289. client_id = VGA_SWITCHEROO_IGD;
  290. }
  291. if (strncmp(usercmd, "MDIS", 4) == 0) {
  292. just_mux = true;
  293. client_id = VGA_SWITCHEROO_DIS;
  294. }
  295. if (client_id == -1)
  296. goto out;
  297. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  298. if (vgasr_priv.clients[i].id == client_id) {
  299. client = &vgasr_priv.clients[i];
  300. break;
  301. }
  302. }
  303. vgasr_priv.delayed_switch_active = false;
  304. if (just_mux) {
  305. ret = vgasr_priv.handler->switchto(client_id);
  306. goto out;
  307. }
  308. if (client->active == true)
  309. goto out;
  310. /* okay we want a switch - test if devices are willing to switch */
  311. can_switch = true;
  312. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  313. can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
  314. if (can_switch == false) {
  315. printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
  316. break;
  317. }
  318. }
  319. if (can_switch == false && delay == false)
  320. goto out;
  321. if (can_switch == true) {
  322. pdev_name = pci_name(client->pdev);
  323. ret = vga_switchto_stage1(client);
  324. if (ret)
  325. printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
  326. ret = vga_switchto_stage2(client);
  327. if (ret)
  328. printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
  329. } else {
  330. printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
  331. vgasr_priv.delayed_switch_active = true;
  332. vgasr_priv.delayed_client_id = client_id;
  333. ret = vga_switchto_stage1(client);
  334. if (ret)
  335. printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
  336. }
  337. out:
  338. mutex_unlock(&vgasr_mutex);
  339. return cnt;
  340. }
  341. static const struct file_operations vga_switcheroo_debugfs_fops = {
  342. .owner = THIS_MODULE,
  343. .open = vga_switcheroo_debugfs_open,
  344. .write = vga_switcheroo_debugfs_write,
  345. .read = seq_read,
  346. .llseek = seq_lseek,
  347. .release = single_release,
  348. };
  349. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
  350. {
  351. if (priv->switch_file) {
  352. debugfs_remove(priv->switch_file);
  353. priv->switch_file = NULL;
  354. }
  355. if (priv->debugfs_root) {
  356. debugfs_remove(priv->debugfs_root);
  357. priv->debugfs_root = NULL;
  358. }
  359. }
  360. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
  361. {
  362. /* already initialised */
  363. if (priv->debugfs_root)
  364. return 0;
  365. priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
  366. if (!priv->debugfs_root) {
  367. printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
  368. goto fail;
  369. }
  370. priv->switch_file = debugfs_create_file("switch", 0644,
  371. priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
  372. if (!priv->switch_file) {
  373. printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
  374. goto fail;
  375. }
  376. return 0;
  377. fail:
  378. vga_switcheroo_debugfs_fini(priv);
  379. return -1;
  380. }
  381. int vga_switcheroo_process_delayed_switch(void)
  382. {
  383. struct vga_switcheroo_client *client = NULL;
  384. const char *pdev_name;
  385. bool can_switch = true;
  386. int i;
  387. int ret;
  388. int err = -EINVAL;
  389. mutex_lock(&vgasr_mutex);
  390. if (!vgasr_priv.delayed_switch_active)
  391. goto err;
  392. printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
  393. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  394. if (vgasr_priv.clients[i].id == vgasr_priv.delayed_client_id)
  395. client = &vgasr_priv.clients[i];
  396. can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
  397. if (can_switch == false) {
  398. printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
  399. break;
  400. }
  401. }
  402. if (can_switch == false || client == NULL)
  403. goto err;
  404. pdev_name = pci_name(client->pdev);
  405. ret = vga_switchto_stage2(client);
  406. if (ret)
  407. printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
  408. vgasr_priv.delayed_switch_active = false;
  409. err = 0;
  410. err:
  411. mutex_unlock(&vgasr_mutex);
  412. return err;
  413. }
  414. EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);