vga_switcheroo.c 29 KB

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