vga_switcheroo.c 12 KB

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