vga_switcheroo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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:%c:%s:%s\n", i,
  148. vgasr_priv.clients[i].active ? '+' : ' ',
  149. vgasr_priv.clients[i].pwr_state ? "Pwr" : "Off",
  150. pci_name(vgasr_priv.clients[i].pdev));
  151. }
  152. mutex_unlock(&vgasr_mutex);
  153. return 0;
  154. }
  155. static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
  156. {
  157. return single_open(file, vga_switcheroo_show, NULL);
  158. }
  159. static int vga_switchon(struct vga_switcheroo_client *client)
  160. {
  161. int ret;
  162. ret = vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
  163. /* call the driver callback to turn on device */
  164. client->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
  165. client->pwr_state = VGA_SWITCHEROO_ON;
  166. return 0;
  167. }
  168. static int vga_switchoff(struct vga_switcheroo_client *client)
  169. {
  170. /* call the driver callback to turn off device */
  171. client->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
  172. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
  173. client->pwr_state = VGA_SWITCHEROO_OFF;
  174. return 0;
  175. }
  176. static int vga_switchto(struct vga_switcheroo_client *new_client)
  177. {
  178. int ret;
  179. int i;
  180. struct vga_switcheroo_client *active = NULL;
  181. if (new_client->active == true)
  182. return 0;
  183. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  184. if (vgasr_priv.clients[i].active == true) {
  185. active = &vgasr_priv.clients[i];
  186. break;
  187. }
  188. }
  189. if (!active)
  190. return 0;
  191. /* power up the first device */
  192. ret = pci_enable_device(new_client->pdev);
  193. if (ret)
  194. return ret;
  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->active = false;
  199. active->pdev->resource[PCI_ROM_RESOURCE].flags &= ~IORESOURCE_ROM_SHADOW;
  200. new_client->pdev->resource[PCI_ROM_RESOURCE].flags |= IORESOURCE_ROM_SHADOW;
  201. if (new_client->fb_info) {
  202. struct fb_event event;
  203. event.info = new_client->fb_info;
  204. fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
  205. }
  206. ret = vgasr_priv.handler->switchto(new_client->id);
  207. if (ret)
  208. return ret;
  209. if (active->pwr_state == VGA_SWITCHEROO_ON)
  210. vga_switchoff(active);
  211. new_client->active = true;
  212. return 0;
  213. }
  214. static ssize_t
  215. vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
  216. size_t cnt, loff_t *ppos)
  217. {
  218. char usercmd[64];
  219. const char *pdev_name;
  220. int i, ret;
  221. bool delay = false, can_switch;
  222. int client_id = -1;
  223. struct vga_switcheroo_client *client = NULL;
  224. if (cnt > 63)
  225. cnt = 63;
  226. if (copy_from_user(usercmd, ubuf, cnt))
  227. return -EFAULT;
  228. mutex_lock(&vgasr_mutex);
  229. if (!vgasr_priv.active) {
  230. cnt = -EINVAL;
  231. goto out;
  232. }
  233. /* pwr off the device not in use */
  234. if (strncmp(usercmd, "OFF", 3) == 0) {
  235. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  236. if (vgasr_priv.clients[i].active)
  237. continue;
  238. if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_ON)
  239. vga_switchoff(&vgasr_priv.clients[i]);
  240. }
  241. goto out;
  242. }
  243. /* pwr on the device not in use */
  244. if (strncmp(usercmd, "ON", 2) == 0) {
  245. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  246. if (vgasr_priv.clients[i].active)
  247. continue;
  248. if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_OFF)
  249. vga_switchon(&vgasr_priv.clients[i]);
  250. }
  251. goto out;
  252. }
  253. /* request a delayed switch - test can we switch now */
  254. if (strncmp(usercmd, "DIGD", 4) == 0) {
  255. client_id = VGA_SWITCHEROO_IGD;
  256. delay = true;
  257. }
  258. if (strncmp(usercmd, "DDIS", 4) == 0) {
  259. client_id = VGA_SWITCHEROO_DIS;
  260. delay = true;
  261. }
  262. if (strncmp(usercmd, "IGD", 3) == 0)
  263. client_id = VGA_SWITCHEROO_IGD;
  264. if (strncmp(usercmd, "DIS", 3) == 0)
  265. client_id = VGA_SWITCHEROO_DIS;
  266. if (client_id == -1)
  267. goto out;
  268. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  269. if (vgasr_priv.clients[i].id == client_id) {
  270. client = &vgasr_priv.clients[i];
  271. break;
  272. }
  273. }
  274. vgasr_priv.delayed_switch_active = false;
  275. /* okay we want a switch - test if devices are willing to switch */
  276. can_switch = true;
  277. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  278. can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
  279. if (can_switch == false) {
  280. printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
  281. break;
  282. }
  283. }
  284. if (can_switch == false && delay == false)
  285. goto out;
  286. if (can_switch == true) {
  287. pdev_name = pci_name(client->pdev);
  288. ret = vga_switchto(client);
  289. if (ret)
  290. printk(KERN_ERR "vga_switcheroo: switching failed %d\n", ret);
  291. } else {
  292. printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
  293. vgasr_priv.delayed_switch_active = true;
  294. vgasr_priv.delayed_client_id = client_id;
  295. /* we should at least power up the card to
  296. make the switch faster */
  297. if (client->pwr_state == VGA_SWITCHEROO_OFF)
  298. vga_switchon(client);
  299. }
  300. out:
  301. mutex_unlock(&vgasr_mutex);
  302. return cnt;
  303. }
  304. static const struct file_operations vga_switcheroo_debugfs_fops = {
  305. .owner = THIS_MODULE,
  306. .open = vga_switcheroo_debugfs_open,
  307. .write = vga_switcheroo_debugfs_write,
  308. .read = seq_read,
  309. .llseek = seq_lseek,
  310. .release = single_release,
  311. };
  312. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
  313. {
  314. if (priv->switch_file) {
  315. debugfs_remove(priv->switch_file);
  316. priv->switch_file = NULL;
  317. }
  318. if (priv->debugfs_root) {
  319. debugfs_remove(priv->debugfs_root);
  320. priv->debugfs_root = NULL;
  321. }
  322. }
  323. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
  324. {
  325. /* already initialised */
  326. if (priv->debugfs_root)
  327. return 0;
  328. priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
  329. if (!priv->debugfs_root) {
  330. printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
  331. goto fail;
  332. }
  333. priv->switch_file = debugfs_create_file("switch", 0644,
  334. priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
  335. if (!priv->switch_file) {
  336. printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
  337. goto fail;
  338. }
  339. return 0;
  340. fail:
  341. vga_switcheroo_debugfs_fini(priv);
  342. return -1;
  343. }
  344. int vga_switcheroo_process_delayed_switch(void)
  345. {
  346. struct vga_switcheroo_client *client = NULL;
  347. const char *pdev_name;
  348. bool can_switch = true;
  349. int i;
  350. int ret;
  351. int err = -EINVAL;
  352. mutex_lock(&vgasr_mutex);
  353. if (!vgasr_priv.delayed_switch_active)
  354. goto err;
  355. printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
  356. for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
  357. if (vgasr_priv.clients[i].id == vgasr_priv.delayed_client_id)
  358. client = &vgasr_priv.clients[i];
  359. can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
  360. if (can_switch == false) {
  361. printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
  362. break;
  363. }
  364. }
  365. if (can_switch == false || client == NULL)
  366. goto err;
  367. pdev_name = pci_name(client->pdev);
  368. ret = vga_switchto(client);
  369. if (ret)
  370. printk(KERN_ERR "vga_switcheroo: delayed switching failed %d\n", ret);
  371. vgasr_priv.delayed_switch_active = false;
  372. err = 0;
  373. err:
  374. mutex_unlock(&vgasr_mutex);
  375. return err;
  376. }
  377. EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);