vga_switcheroo.c 29 KB

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