vga_switcheroo.c 14 KB

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