vga_switcheroo.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
  3. *
  4. * Copyright (c) 2010 Red Hat Inc.
  5. * Author : Dave Airlie <airlied@redhat.com>
  6. *
  7. * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS
  27. * IN THE SOFTWARE.
  28. *
  29. */
  30. #define pr_fmt(fmt) "vga_switcheroo: " fmt
  31. #include <linux/console.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/fb.h>
  34. #include <linux/fs.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/vgaarb.h>
  41. #include <linux/vga_switcheroo.h>
  42. /**
  43. * DOC: Overview
  44. *
  45. * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
  46. * These come in two flavors:
  47. *
  48. * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
  49. * * muxless: Dual GPUs but only one of them is connected to outputs.
  50. * The other one is merely used to offload rendering, its results
  51. * are copied over PCIe into the framebuffer. On Linux this is
  52. * supported with DRI PRIME.
  53. *
  54. * Hybrid graphics started to appear in the late Naughties and were initially
  55. * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
  56. * A notable exception is the MacBook Pro which continues to use a mux.
  57. * Muxes come with varying capabilities: Some switch only the panel, others
  58. * can also switch external displays. Some switch all display pins at once
  59. * while others can switch just the DDC lines. (To allow EDID probing
  60. * for the inactive GPU.) Also, muxes are often used to cut power to the
  61. * discrete GPU while it is not used.
  62. *
  63. * DRM drivers register GPUs with vga_switcheroo, these are heretoforth called
  64. * clients. The mux is called the handler. Muxless machines also register a
  65. * handler to control the power state of the discrete GPU, its ->switchto
  66. * callback is a no-op for obvious reasons. The discrete GPU is often equipped
  67. * with an HDA controller for the HDMI/DP audio signal, this will also
  68. * register as a client so that vga_switcheroo can take care of the correct
  69. * suspend/resume order when changing the discrete GPU's power state. In total
  70. * there can thus be up to three clients: Two vga clients (GPUs) and one audio
  71. * client (on the discrete GPU). The code is mostly prepared to support
  72. * machines with more than two GPUs should they become available.
  73. * The GPU to which the outputs are currently switched is called the
  74. * active client in vga_switcheroo parlance. The GPU not in use is the
  75. * inactive client.
  76. */
  77. /**
  78. * struct vga_switcheroo_client - registered client
  79. * @pdev: client pci device
  80. * @fb_info: framebuffer to which console is remapped on switching
  81. * @pwr_state: current power state
  82. * @ops: client callbacks
  83. * @id: client identifier, see enum vga_switcheroo_client_id.
  84. * Determining the id requires the handler, so GPUs are initially
  85. * assigned -1 and later given their true id in vga_switcheroo_enable()
  86. * @active: whether the outputs are currently switched to this client
  87. * @driver_power_control: whether power state is controlled by the driver's
  88. * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
  89. * interface is a no-op so as not to interfere with runtime pm
  90. * @list: client list
  91. *
  92. * Registered client. A client can be either a GPU or an audio device on a GPU.
  93. * For audio clients, the @fb_info, @active and @driver_power_control members
  94. * are bogus.
  95. */
  96. struct vga_switcheroo_client {
  97. struct pci_dev *pdev;
  98. struct fb_info *fb_info;
  99. int pwr_state;
  100. const struct vga_switcheroo_client_ops *ops;
  101. int id;
  102. bool active;
  103. bool driver_power_control;
  104. struct list_head list;
  105. };
  106. /*
  107. * protects access to struct vgasr_priv
  108. */
  109. static DEFINE_MUTEX(vgasr_mutex);
  110. /**
  111. * struct vgasr_priv - vga_switcheroo private data
  112. * @active: whether vga_switcheroo is enabled.
  113. * Prerequisite is the registration of two GPUs and a handler
  114. * @delayed_switch_active: whether a delayed switch is pending
  115. * @delayed_client_id: client to which a delayed switch is pending
  116. * @debugfs_root: directory for vga_switcheroo debugfs interface
  117. * @switch_file: file for vga_switcheroo debugfs interface
  118. * @registered_clients: number of registered GPUs
  119. * (counting only vga clients, not audio clients)
  120. * @clients: list of registered clients
  121. * @handler: registered handler
  122. *
  123. * vga_switcheroo private data. Currently only one vga_switcheroo instance
  124. * per system is supported.
  125. */
  126. struct vgasr_priv {
  127. bool active;
  128. bool delayed_switch_active;
  129. enum vga_switcheroo_client_id delayed_client_id;
  130. struct dentry *debugfs_root;
  131. struct dentry *switch_file;
  132. int registered_clients;
  133. struct list_head clients;
  134. struct vga_switcheroo_handler *handler;
  135. };
  136. #define ID_BIT_AUDIO 0x100
  137. #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
  138. #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
  139. #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
  140. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
  141. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
  142. /* only one switcheroo per system */
  143. static struct vgasr_priv vgasr_priv = {
  144. .clients = LIST_HEAD_INIT(vgasr_priv.clients),
  145. };
  146. static bool vga_switcheroo_ready(void)
  147. {
  148. /* we're ready if we get two clients + handler */
  149. return !vgasr_priv.active &&
  150. vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
  151. }
  152. static void vga_switcheroo_enable(void)
  153. {
  154. int ret;
  155. struct vga_switcheroo_client *client;
  156. /* call the handler to init */
  157. if (vgasr_priv.handler->init)
  158. vgasr_priv.handler->init();
  159. list_for_each_entry(client, &vgasr_priv.clients, list) {
  160. if (client->id != -1)
  161. continue;
  162. ret = vgasr_priv.handler->get_client_id(client->pdev);
  163. if (ret < 0)
  164. return;
  165. client->id = ret;
  166. }
  167. vga_switcheroo_debugfs_init(&vgasr_priv);
  168. vgasr_priv.active = true;
  169. }
  170. /**
  171. * vga_switcheroo_register_handler() - register handler
  172. * @handler: handler callbacks
  173. *
  174. * Register handler. Enable vga_switcheroo if two vga clients have already
  175. * registered.
  176. *
  177. * Return: 0 on success, -EINVAL if a handler was already registered.
  178. */
  179. int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
  180. {
  181. mutex_lock(&vgasr_mutex);
  182. if (vgasr_priv.handler) {
  183. mutex_unlock(&vgasr_mutex);
  184. return -EINVAL;
  185. }
  186. vgasr_priv.handler = handler;
  187. if (vga_switcheroo_ready()) {
  188. pr_info("enabled\n");
  189. vga_switcheroo_enable();
  190. }
  191. mutex_unlock(&vgasr_mutex);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(vga_switcheroo_register_handler);
  195. /**
  196. * vga_switcheroo_unregister_handler() - unregister handler
  197. *
  198. * Unregister handler. Disable vga_switcheroo.
  199. */
  200. void vga_switcheroo_unregister_handler(void)
  201. {
  202. mutex_lock(&vgasr_mutex);
  203. vgasr_priv.handler = NULL;
  204. if (vgasr_priv.active) {
  205. pr_info("disabled\n");
  206. vga_switcheroo_debugfs_fini(&vgasr_priv);
  207. vgasr_priv.active = false;
  208. }
  209. mutex_unlock(&vgasr_mutex);
  210. }
  211. EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
  212. static int register_client(struct pci_dev *pdev,
  213. const struct vga_switcheroo_client_ops *ops,
  214. int id, bool active, bool driver_power_control)
  215. {
  216. struct vga_switcheroo_client *client;
  217. client = kzalloc(sizeof(*client), GFP_KERNEL);
  218. if (!client)
  219. return -ENOMEM;
  220. client->pwr_state = VGA_SWITCHEROO_ON;
  221. client->pdev = pdev;
  222. client->ops = ops;
  223. client->id = id;
  224. client->active = active;
  225. client->driver_power_control = driver_power_control;
  226. mutex_lock(&vgasr_mutex);
  227. list_add_tail(&client->list, &vgasr_priv.clients);
  228. if (client_is_vga(client))
  229. vgasr_priv.registered_clients++;
  230. if (vga_switcheroo_ready()) {
  231. pr_info("enabled\n");
  232. vga_switcheroo_enable();
  233. }
  234. mutex_unlock(&vgasr_mutex);
  235. return 0;
  236. }
  237. /**
  238. * vga_switcheroo_register_client - register vga client
  239. * @pdev: client pci device
  240. * @ops: client callbacks
  241. * @driver_power_control: whether power state is controlled by the driver's
  242. * runtime pm
  243. *
  244. * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
  245. * handler have already registered. The power state of the client is assumed
  246. * to be ON.
  247. *
  248. * Return: 0 on success, -ENOMEM on memory allocation error.
  249. */
  250. int vga_switcheroo_register_client(struct pci_dev *pdev,
  251. const struct vga_switcheroo_client_ops *ops,
  252. bool driver_power_control)
  253. {
  254. return register_client(pdev, ops, -1,
  255. pdev == vga_default_device(),
  256. driver_power_control);
  257. }
  258. EXPORT_SYMBOL(vga_switcheroo_register_client);
  259. /**
  260. * vga_switcheroo_register_audio_client - register audio client
  261. * @pdev: client pci device
  262. * @ops: client callbacks
  263. * @id: client identifier, see enum vga_switcheroo_client_id
  264. *
  265. * Register audio client (audio device on a GPU). The power state of the
  266. * client is assumed to be ON.
  267. *
  268. * Return: 0 on success, -ENOMEM on memory allocation error.
  269. */
  270. int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
  271. const struct vga_switcheroo_client_ops *ops,
  272. int id)
  273. {
  274. return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
  275. }
  276. EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
  277. static struct vga_switcheroo_client *
  278. find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
  279. {
  280. struct vga_switcheroo_client *client;
  281. list_for_each_entry(client, head, list)
  282. if (client->pdev == pdev)
  283. return client;
  284. return NULL;
  285. }
  286. static struct vga_switcheroo_client *
  287. find_client_from_id(struct list_head *head, int client_id)
  288. {
  289. struct vga_switcheroo_client *client;
  290. list_for_each_entry(client, head, list)
  291. if (client->id == client_id)
  292. return client;
  293. return NULL;
  294. }
  295. static struct vga_switcheroo_client *
  296. find_active_client(struct list_head *head)
  297. {
  298. struct vga_switcheroo_client *client;
  299. list_for_each_entry(client, head, list)
  300. if (client->active)
  301. return client;
  302. return NULL;
  303. }
  304. /**
  305. * vga_switcheroo_get_client_state() - obtain power state of a given client
  306. * @pdev: client pci device
  307. *
  308. * Obtain power state of a given client as seen from vga_switcheroo.
  309. * The function is only called from hda_intel.c.
  310. *
  311. * Return: Power state.
  312. */
  313. int vga_switcheroo_get_client_state(struct pci_dev *pdev)
  314. {
  315. struct vga_switcheroo_client *client;
  316. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  317. if (!client)
  318. return VGA_SWITCHEROO_NOT_FOUND;
  319. if (!vgasr_priv.active)
  320. return VGA_SWITCHEROO_INIT;
  321. return client->pwr_state;
  322. }
  323. EXPORT_SYMBOL(vga_switcheroo_get_client_state);
  324. /**
  325. * vga_switcheroo_unregister_client() - unregister client
  326. * @pdev: client pci device
  327. *
  328. * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
  329. */
  330. void vga_switcheroo_unregister_client(struct pci_dev *pdev)
  331. {
  332. struct vga_switcheroo_client *client;
  333. mutex_lock(&vgasr_mutex);
  334. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  335. if (client) {
  336. if (client_is_vga(client))
  337. vgasr_priv.registered_clients--;
  338. list_del(&client->list);
  339. kfree(client);
  340. }
  341. if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
  342. pr_info("disabled\n");
  343. vga_switcheroo_debugfs_fini(&vgasr_priv);
  344. vgasr_priv.active = false;
  345. }
  346. mutex_unlock(&vgasr_mutex);
  347. }
  348. EXPORT_SYMBOL(vga_switcheroo_unregister_client);
  349. /**
  350. * vga_switcheroo_client_fb_set() - set framebuffer of a given client
  351. * @pdev: client pci device
  352. * @info: framebuffer
  353. *
  354. * Set framebuffer of a given client. The console will be remapped to this
  355. * on switching.
  356. */
  357. void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
  358. struct fb_info *info)
  359. {
  360. struct vga_switcheroo_client *client;
  361. mutex_lock(&vgasr_mutex);
  362. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  363. if (client)
  364. client->fb_info = info;
  365. mutex_unlock(&vgasr_mutex);
  366. }
  367. EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
  368. /**
  369. * DOC: Manual switching and manual power control
  370. *
  371. * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
  372. * can be read to retrieve the current vga_switcheroo state and commands
  373. * can be written to it to change the state. The file appears as soon as
  374. * two GPU drivers and one handler have registered with vga_switcheroo.
  375. * The following commands are understood:
  376. *
  377. * * OFF: Power off the device not in use.
  378. * * ON: Power on the device not in use.
  379. * * IGD: Switch to the integrated graphics device.
  380. * Power on the integrated GPU if necessary, power off the discrete GPU.
  381. * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
  382. * have opened device files of the GPUs or the audio client. If the
  383. * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
  384. * and /dev/snd/controlC1 to identify processes blocking the switch.
  385. * * DIS: Switch to the discrete graphics device.
  386. * * DIGD: Delayed switch to the integrated graphics device.
  387. * This will perform the switch once the last user space process has
  388. * closed the device files of the GPUs and the audio client.
  389. * * DDIS: Delayed switch to the discrete graphics device.
  390. * * MIGD: Mux-only switch to the integrated graphics device.
  391. * Does not remap console or change the power state of either gpu.
  392. * If the integrated GPU is currently off, the screen will turn black.
  393. * If it is on, the screen will show whatever happens to be in VRAM.
  394. * Either way, the user has to blindly enter the command to switch back.
  395. * * MDIS: Mux-only switch to the discrete graphics device.
  396. *
  397. * For GPUs whose power state is controlled by the driver's runtime pm,
  398. * the ON and OFF commands are a no-op (see next section).
  399. *
  400. * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
  401. * should not be used.
  402. */
  403. static int vga_switcheroo_show(struct seq_file *m, void *v)
  404. {
  405. struct vga_switcheroo_client *client;
  406. int i = 0;
  407. mutex_lock(&vgasr_mutex);
  408. list_for_each_entry(client, &vgasr_priv.clients, list) {
  409. seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
  410. client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
  411. "IGD",
  412. client_is_vga(client) ? "" : "-Audio",
  413. client->active ? '+' : ' ',
  414. client->driver_power_control ? "Dyn" : "",
  415. client->pwr_state ? "Pwr" : "Off",
  416. pci_name(client->pdev));
  417. i++;
  418. }
  419. mutex_unlock(&vgasr_mutex);
  420. return 0;
  421. }
  422. static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
  423. {
  424. return single_open(file, vga_switcheroo_show, NULL);
  425. }
  426. static int vga_switchon(struct vga_switcheroo_client *client)
  427. {
  428. if (client->driver_power_control)
  429. return 0;
  430. if (vgasr_priv.handler->power_state)
  431. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
  432. /* call the driver callback to turn on device */
  433. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
  434. client->pwr_state = VGA_SWITCHEROO_ON;
  435. return 0;
  436. }
  437. static int vga_switchoff(struct vga_switcheroo_client *client)
  438. {
  439. if (client->driver_power_control)
  440. return 0;
  441. /* call the driver callback to turn off device */
  442. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
  443. if (vgasr_priv.handler->power_state)
  444. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
  445. client->pwr_state = VGA_SWITCHEROO_OFF;
  446. return 0;
  447. }
  448. static void set_audio_state(int id, int state)
  449. {
  450. struct vga_switcheroo_client *client;
  451. client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
  452. if (client && client->pwr_state != state) {
  453. client->ops->set_gpu_state(client->pdev, state);
  454. client->pwr_state = state;
  455. }
  456. }
  457. /* stage one happens before delay */
  458. static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
  459. {
  460. struct vga_switcheroo_client *active;
  461. active = find_active_client(&vgasr_priv.clients);
  462. if (!active)
  463. return 0;
  464. if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
  465. vga_switchon(new_client);
  466. vga_set_default_device(new_client->pdev);
  467. return 0;
  468. }
  469. /* post delay */
  470. static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
  471. {
  472. int ret;
  473. struct vga_switcheroo_client *active;
  474. active = find_active_client(&vgasr_priv.clients);
  475. if (!active)
  476. return 0;
  477. active->active = false;
  478. set_audio_state(active->id, VGA_SWITCHEROO_OFF);
  479. if (new_client->fb_info) {
  480. struct fb_event event;
  481. console_lock();
  482. event.info = new_client->fb_info;
  483. fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
  484. console_unlock();
  485. }
  486. ret = vgasr_priv.handler->switchto(new_client->id);
  487. if (ret)
  488. return ret;
  489. if (new_client->ops->reprobe)
  490. new_client->ops->reprobe(new_client->pdev);
  491. if (active->pwr_state == VGA_SWITCHEROO_ON)
  492. vga_switchoff(active);
  493. set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
  494. new_client->active = true;
  495. return 0;
  496. }
  497. static bool check_can_switch(void)
  498. {
  499. struct vga_switcheroo_client *client;
  500. list_for_each_entry(client, &vgasr_priv.clients, list) {
  501. if (!client->ops->can_switch(client->pdev)) {
  502. pr_err("client %x refused switch\n", client->id);
  503. return false;
  504. }
  505. }
  506. return true;
  507. }
  508. static ssize_t
  509. vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
  510. size_t cnt, loff_t *ppos)
  511. {
  512. char usercmd[64];
  513. int ret;
  514. bool delay = false, can_switch;
  515. bool just_mux = false;
  516. int client_id = -1;
  517. struct vga_switcheroo_client *client = NULL;
  518. if (cnt > 63)
  519. cnt = 63;
  520. if (copy_from_user(usercmd, ubuf, cnt))
  521. return -EFAULT;
  522. mutex_lock(&vgasr_mutex);
  523. if (!vgasr_priv.active) {
  524. cnt = -EINVAL;
  525. goto out;
  526. }
  527. /* pwr off the device not in use */
  528. if (strncmp(usercmd, "OFF", 3) == 0) {
  529. list_for_each_entry(client, &vgasr_priv.clients, list) {
  530. if (client->active || client_is_audio(client))
  531. continue;
  532. if (client->driver_power_control)
  533. continue;
  534. set_audio_state(client->id, VGA_SWITCHEROO_OFF);
  535. if (client->pwr_state == VGA_SWITCHEROO_ON)
  536. vga_switchoff(client);
  537. }
  538. goto out;
  539. }
  540. /* pwr on the device not in use */
  541. if (strncmp(usercmd, "ON", 2) == 0) {
  542. list_for_each_entry(client, &vgasr_priv.clients, list) {
  543. if (client->active || client_is_audio(client))
  544. continue;
  545. if (client->driver_power_control)
  546. continue;
  547. if (client->pwr_state == VGA_SWITCHEROO_OFF)
  548. vga_switchon(client);
  549. set_audio_state(client->id, VGA_SWITCHEROO_ON);
  550. }
  551. goto out;
  552. }
  553. /* request a delayed switch - test can we switch now */
  554. if (strncmp(usercmd, "DIGD", 4) == 0) {
  555. client_id = VGA_SWITCHEROO_IGD;
  556. delay = true;
  557. }
  558. if (strncmp(usercmd, "DDIS", 4) == 0) {
  559. client_id = VGA_SWITCHEROO_DIS;
  560. delay = true;
  561. }
  562. if (strncmp(usercmd, "IGD", 3) == 0)
  563. client_id = VGA_SWITCHEROO_IGD;
  564. if (strncmp(usercmd, "DIS", 3) == 0)
  565. client_id = VGA_SWITCHEROO_DIS;
  566. if (strncmp(usercmd, "MIGD", 4) == 0) {
  567. just_mux = true;
  568. client_id = VGA_SWITCHEROO_IGD;
  569. }
  570. if (strncmp(usercmd, "MDIS", 4) == 0) {
  571. just_mux = true;
  572. client_id = VGA_SWITCHEROO_DIS;
  573. }
  574. if (client_id == -1)
  575. goto out;
  576. client = find_client_from_id(&vgasr_priv.clients, client_id);
  577. if (!client)
  578. goto out;
  579. vgasr_priv.delayed_switch_active = false;
  580. if (just_mux) {
  581. ret = vgasr_priv.handler->switchto(client_id);
  582. goto out;
  583. }
  584. if (client->active)
  585. goto out;
  586. /* okay we want a switch - test if devices are willing to switch */
  587. can_switch = check_can_switch();
  588. if (can_switch == false && delay == false)
  589. goto out;
  590. if (can_switch) {
  591. ret = vga_switchto_stage1(client);
  592. if (ret)
  593. pr_err("switching failed stage 1 %d\n", ret);
  594. ret = vga_switchto_stage2(client);
  595. if (ret)
  596. pr_err("switching failed stage 2 %d\n", ret);
  597. } else {
  598. pr_info("setting delayed switch to client %d\n", client->id);
  599. vgasr_priv.delayed_switch_active = true;
  600. vgasr_priv.delayed_client_id = client_id;
  601. ret = vga_switchto_stage1(client);
  602. if (ret)
  603. pr_err("delayed switching stage 1 failed %d\n", ret);
  604. }
  605. out:
  606. mutex_unlock(&vgasr_mutex);
  607. return cnt;
  608. }
  609. static const struct file_operations vga_switcheroo_debugfs_fops = {
  610. .owner = THIS_MODULE,
  611. .open = vga_switcheroo_debugfs_open,
  612. .write = vga_switcheroo_debugfs_write,
  613. .read = seq_read,
  614. .llseek = seq_lseek,
  615. .release = single_release,
  616. };
  617. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
  618. {
  619. debugfs_remove(priv->switch_file);
  620. priv->switch_file = NULL;
  621. debugfs_remove(priv->debugfs_root);
  622. priv->debugfs_root = NULL;
  623. }
  624. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
  625. {
  626. static const char mp[] = "/sys/kernel/debug";
  627. /* already initialised */
  628. if (priv->debugfs_root)
  629. return 0;
  630. priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
  631. if (!priv->debugfs_root) {
  632. pr_err("Cannot create %s/vgaswitcheroo\n", mp);
  633. goto fail;
  634. }
  635. priv->switch_file = debugfs_create_file("switch", 0644,
  636. priv->debugfs_root, NULL,
  637. &vga_switcheroo_debugfs_fops);
  638. if (!priv->switch_file) {
  639. pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
  640. goto fail;
  641. }
  642. return 0;
  643. fail:
  644. vga_switcheroo_debugfs_fini(priv);
  645. return -1;
  646. }
  647. /**
  648. * vga_switcheroo_process_delayed_switch() - helper for delayed switching
  649. *
  650. * Process a delayed switch if one is pending. DRM drivers should call this
  651. * from their ->lastclose callback.
  652. *
  653. * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
  654. * has unregistered in the meantime or if there are other clients blocking the
  655. * switch. If the actual switch fails, an error is reported and 0 is returned.
  656. */
  657. int vga_switcheroo_process_delayed_switch(void)
  658. {
  659. struct vga_switcheroo_client *client;
  660. int ret;
  661. int err = -EINVAL;
  662. mutex_lock(&vgasr_mutex);
  663. if (!vgasr_priv.delayed_switch_active)
  664. goto err;
  665. pr_info("processing delayed switch to %d\n",
  666. vgasr_priv.delayed_client_id);
  667. client = find_client_from_id(&vgasr_priv.clients,
  668. vgasr_priv.delayed_client_id);
  669. if (!client || !check_can_switch())
  670. goto err;
  671. ret = vga_switchto_stage2(client);
  672. if (ret)
  673. pr_err("delayed switching failed stage 2 %d\n", ret);
  674. vgasr_priv.delayed_switch_active = false;
  675. err = 0;
  676. err:
  677. mutex_unlock(&vgasr_mutex);
  678. return err;
  679. }
  680. EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
  681. /**
  682. * DOC: Driver power control
  683. *
  684. * In this mode of use, the discrete GPU automatically powers up and down at
  685. * the discretion of the driver's runtime pm. On muxed machines, the user may
  686. * still influence the muxer state by way of the debugfs interface, however
  687. * the ON and OFF commands become a no-op for the discrete GPU.
  688. *
  689. * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
  690. * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
  691. * command line disables it.
  692. *
  693. * When the driver decides to power up or down, it notifies vga_switcheroo
  694. * thereof so that it can (a) power the audio device on the GPU up or down,
  695. * and (b) update its internal power state representation for the device.
  696. * This is achieved by vga_switcheroo_set_dynamic_switch().
  697. *
  698. * After the GPU has been suspended, the handler needs to be called to cut
  699. * power to the GPU. Likewise it needs to reinstate power before the GPU
  700. * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
  701. * which augments the GPU's suspend/resume functions by the requisite
  702. * calls to the handler.
  703. *
  704. * When the audio device resumes, the GPU needs to be woken. This is achieved
  705. * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
  706. * audio device's resume function.
  707. *
  708. * On muxed machines, if the mux is initially switched to the discrete GPU,
  709. * the user ends up with a black screen when the GPU powers down after boot.
  710. * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
  711. * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
  712. */
  713. static void vga_switcheroo_power_switch(struct pci_dev *pdev,
  714. enum vga_switcheroo_state state)
  715. {
  716. struct vga_switcheroo_client *client;
  717. if (!vgasr_priv.handler->power_state)
  718. return;
  719. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  720. if (!client)
  721. return;
  722. if (!client->driver_power_control)
  723. return;
  724. vgasr_priv.handler->power_state(client->id, state);
  725. }
  726. /**
  727. * vga_switcheroo_set_dynamic_switch() - helper for driver power control
  728. * @pdev: client pci device
  729. * @dynamic: new power state
  730. *
  731. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  732. * When the driver decides to power up or down, it notifies vga_switcheroo
  733. * thereof using this helper so that it can (a) power the audio device on
  734. * the GPU up or down, and (b) update its internal power state representation
  735. * for the device.
  736. */
  737. void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
  738. enum vga_switcheroo_state dynamic)
  739. {
  740. struct vga_switcheroo_client *client;
  741. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  742. if (!client)
  743. return;
  744. if (!client->driver_power_control)
  745. return;
  746. client->pwr_state = dynamic;
  747. set_audio_state(client->id, dynamic);
  748. }
  749. EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
  750. /* switcheroo power domain */
  751. static int vga_switcheroo_runtime_suspend(struct device *dev)
  752. {
  753. struct pci_dev *pdev = to_pci_dev(dev);
  754. int ret;
  755. ret = dev->bus->pm->runtime_suspend(dev);
  756. if (ret)
  757. return ret;
  758. if (vgasr_priv.handler->switchto)
  759. vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
  760. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
  761. return 0;
  762. }
  763. static int vga_switcheroo_runtime_resume(struct device *dev)
  764. {
  765. struct pci_dev *pdev = to_pci_dev(dev);
  766. int ret;
  767. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
  768. ret = dev->bus->pm->runtime_resume(dev);
  769. if (ret)
  770. return ret;
  771. return 0;
  772. }
  773. /**
  774. * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
  775. * @dev: vga client device
  776. * @domain: power domain
  777. *
  778. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  779. * After the GPU has been suspended, the handler needs to be called to cut
  780. * power to the GPU. Likewise it needs to reinstate power before the GPU
  781. * can resume. To this end, this helper augments the suspend/resume functions
  782. * by the requisite calls to the handler. It needs only be called on platforms
  783. * where the power switch is separate to the device being powered down.
  784. */
  785. int vga_switcheroo_init_domain_pm_ops(struct device *dev,
  786. struct dev_pm_domain *domain)
  787. {
  788. /* copy over all the bus versions */
  789. if (dev->bus && dev->bus->pm) {
  790. domain->ops = *dev->bus->pm;
  791. domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
  792. domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
  793. dev->pm_domain = domain;
  794. return 0;
  795. }
  796. dev->pm_domain = NULL;
  797. return -EINVAL;
  798. }
  799. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
  800. void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
  801. {
  802. dev->pm_domain = NULL;
  803. }
  804. EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
  805. static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
  806. {
  807. struct pci_dev *pdev = to_pci_dev(dev);
  808. int ret;
  809. struct vga_switcheroo_client *client, *found = NULL;
  810. /* we need to check if we have to switch back on the video
  811. device so the audio device can come back */
  812. list_for_each_entry(client, &vgasr_priv.clients, list) {
  813. if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
  814. client_is_vga(client)) {
  815. found = client;
  816. ret = pm_runtime_get_sync(&client->pdev->dev);
  817. if (ret) {
  818. if (ret != 1)
  819. return ret;
  820. }
  821. break;
  822. }
  823. }
  824. ret = dev->bus->pm->runtime_resume(dev);
  825. /* put the reference for the gpu */
  826. if (found) {
  827. pm_runtime_mark_last_busy(&found->pdev->dev);
  828. pm_runtime_put_autosuspend(&found->pdev->dev);
  829. }
  830. return ret;
  831. }
  832. /**
  833. * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
  834. * power control
  835. * @dev: audio client device
  836. * @domain: power domain
  837. *
  838. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  839. * When the audio device resumes, the GPU needs to be woken. This helper
  840. * augments the audio device's resume function to do that.
  841. *
  842. * Return: 0 on success, -EINVAL if no power management operations are
  843. * defined for this device.
  844. */
  845. int
  846. vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
  847. struct dev_pm_domain *domain)
  848. {
  849. /* copy over all the bus versions */
  850. if (dev->bus && dev->bus->pm) {
  851. domain->ops = *dev->bus->pm;
  852. domain->ops.runtime_resume =
  853. vga_switcheroo_runtime_resume_hdmi_audio;
  854. dev->pm_domain = domain;
  855. return 0;
  856. }
  857. dev->pm_domain = NULL;
  858. return -EINVAL;
  859. }
  860. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);