vga_switcheroo.c 12 KB

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