Lines Matching +full:vga +full:- +full:format

1 // SPDX-License-Identifier: MIT
3 * vgaarb.c: Implements the VGA arbitration. For details refer to
40 * We keep a list of all vga devices in the system to speed
102 /* this is only used a cookie - it should not be dereferenced */
111 if (pdev == vgadev->pdev) in vgadev_find()
117 * vga_default_device - return the default VGA device, for vgacon
121 * vga card setups and/or x86 platforms.
123 * If your VGA default device is not PCI, you'll have to return
126 * hooks for enabling/disabling the VGA default device if that is
127 * possible. This may be a problem with real _ISA_ VGA cards, in
150 * vga_remove_vgacon - deactivete vga console
152 * Unbind and unregister vgacon in case pdev is the default vga
154 * sure vga register access done by vgacon will not disturb the
167 return -ENODEV; in vga_remove_vgacon()
176 vgaarb_info(&pdev->dev, "deactivate vga console\n"); in vga_remove_vgacon()
181 MAX_NR_CONSOLES - 1, 1); in vga_remove_vgacon()
186 if (ret == -ENODEV) in vga_remove_vgacon()
196 /* If we don't ever use VGA arb we should avoid
198 confused about the boot device not being VGA */
202 * VGA arb has occurred and to try and disable resources in vga_check_first_use()
213 struct device *dev = &vgadev->pdev->dev; in __vga_tryget()
223 (vgadev->decodes & VGA_RSRC_LEGACY_IO)) in __vga_tryget()
226 (vgadev->decodes & VGA_RSRC_LEGACY_MEM)) in __vga_tryget()
230 vgaarb_dbg(dev, "%s: owns: %d\n", __func__, vgadev->owns); in __vga_tryget()
233 wants = rsrc & ~vgadev->owns; in __vga_tryget()
259 * VGA forwarding on P2P bridges in __vga_tryget()
261 if (vgadev->pdev->bus != conflict->pdev->bus) { in __vga_tryget()
269 if (conflict->locks & lwants) in __vga_tryget()
276 match = lwants & conflict->owns; in __vga_tryget()
290 if (!conflict->bridge_has_one_vga) { in __vga_tryget()
291 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM) in __vga_tryget()
293 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO) in __vga_tryget()
303 pci_set_vga_state(conflict->pdev, false, pci_bits, flags); in __vga_tryget()
304 conflict->owns &= ~match; in __vga_tryget()
308 conflict->owns &= ~VGA_RSRC_NORMAL_MEM; in __vga_tryget()
310 conflict->owns &= ~VGA_RSRC_NORMAL_IO; in __vga_tryget()
322 if (!vgadev->bridge_has_one_vga) { in __vga_tryget()
332 pci_set_vga_state(vgadev->pdev, true, pci_bits, flags); in __vga_tryget()
334 vgadev->owns |= wants; in __vga_tryget()
336 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK); in __vga_tryget()
338 vgadev->io_lock_cnt++; in __vga_tryget()
340 vgadev->mem_lock_cnt++; in __vga_tryget()
342 vgadev->io_norm_cnt++; in __vga_tryget()
344 vgadev->mem_norm_cnt++; in __vga_tryget()
351 struct device *dev = &vgadev->pdev->dev; in __vga_put()
352 unsigned int old_locks = vgadev->locks; in __vga_put()
359 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) { in __vga_put()
360 vgadev->io_norm_cnt--; in __vga_put()
361 if (vgadev->decodes & VGA_RSRC_LEGACY_IO) in __vga_put()
364 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) { in __vga_put()
365 vgadev->mem_norm_cnt--; in __vga_put()
366 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM) in __vga_put()
369 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0) in __vga_put()
370 vgadev->io_lock_cnt--; in __vga_put()
371 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0) in __vga_put()
372 vgadev->mem_lock_cnt--; in __vga_put()
377 if (vgadev->io_lock_cnt == 0) in __vga_put()
378 vgadev->locks &= ~VGA_RSRC_LEGACY_IO; in __vga_put()
379 if (vgadev->mem_lock_cnt == 0) in __vga_put()
380 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM; in __vga_put()
385 if (old_locks != vgadev->locks) in __vga_put()
390 * vga_get - acquire & locks VGA resources
391 * @pdev: pci device of the VGA card or NULL for the system default
395 * This function acquires VGA resources for the given card and mark those
401 * The arbiter will first look for all VGA cards that might conflict and disable
402 * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
405 * enabled on the card (including VGA forwarding on parent P2P bridges if any).
409 * bridges don't differentiate VGA memory and IO afaik). You can indicate
415 * supported (a per-resource counter is maintained)
417 * On success, release the VGA resource again with vga_put().
442 rc = -ENODEV; in vga_get()
465 rc = -ERESTARTSYS; in vga_get()
476 * vga_tryget - try to acquire & lock legacy VGA resources
477 * @pdev: pci devivce of VGA card or NULL for system default
481 * error (-EBUSY) instead of blocking if the resources are already locked by
484 * On success, release the VGA resource again with vga_put().
506 rc = -ENODEV; in vga_tryget()
510 rc = -EBUSY; in vga_tryget()
517 * vga_put - release lock on legacy VGA resources
518 * @pdev: pci device of VGA card or NULL for system default
597 * Return true if vgadev is a better default VGA device than the best one
603 struct pci_dev *pdev = vgadev->pdev; in vga_is_boot_device()
607 * We select the default VGA device in this order: in vga_is_boot_device()
609 * Legacy VGA device (owns VGA_RSRC_LEGACY_MASK) in vga_is_boot_device()
610 * Non-legacy integrated device (see vga_arb_select_default_device()) in vga_is_boot_device()
611 * Non-legacy discrete device (see vga_arb_select_default_device()) in vga_is_boot_device()
619 if (boot_vga && boot_vga->is_firmware_default) in vga_is_boot_device()
623 vgadev->is_firmware_default = true; in vga_is_boot_device()
628 * A legacy VGA device has MEM and IO enabled and any bridges in vga_is_boot_device()
630 * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are in vga_is_boot_device()
637 (boot_vga->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK) in vga_is_boot_device()
640 if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK) in vga_is_boot_device()
644 * If we haven't found a legacy VGA device, accept a non-legacy in vga_is_boot_device()
647 * use legacy VGA resources. Prefer an integrated GPU over others. in vga_is_boot_device()
653 * An integrated GPU overrides a previous non-legacy in vga_is_boot_device()
658 if (vga_arb_integrated_gpu(&pdev->dev)) in vga_is_boot_device()
662 * We prefer the first non-legacy discrete device we find. in vga_is_boot_device()
666 pci_read_config_word(boot_vga->pdev, PCI_COMMAND, in vga_is_boot_device()
676 * other VGA devices, it is the best candidate so far. in vga_is_boot_device()
685 * Rules for using a bridge to control a VGA descendant decoding: if a bridge
686 * has only one VGA descendant then it can be used to control the VGA routing
688 * control it. If a bridge has a direct VGA descendant, but also have a sub-
689 * bridge VGA descendant then we cannot use that bridge to control the direct
690 * VGA descendant. So for every device we register, we need to iterate all
699 vgadev->bridge_has_one_vga = true; in vga_arbiter_check_bridge_sharing()
702 vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n"); in vga_arbiter_check_bridge_sharing()
707 new_bus = vgadev->pdev->bus; in vga_arbiter_check_bridge_sharing()
709 new_bridge = new_bus->self; in vga_arbiter_check_bridge_sharing()
713 bus = same_bridge_vgadev->pdev->bus; in vga_arbiter_check_bridge_sharing()
714 bridge = bus->self; in vga_arbiter_check_bridge_sharing()
723 same_bridge_vgadev->bridge_has_one_vga = false; in vga_arbiter_check_bridge_sharing()
733 bridge = bus->self; in vga_arbiter_check_bridge_sharing()
735 if (bridge && bridge == vgadev->pdev->bus->self) in vga_arbiter_check_bridge_sharing()
736 vgadev->bridge_has_one_vga = false; in vga_arbiter_check_bridge_sharing()
738 bus = bus->parent; in vga_arbiter_check_bridge_sharing()
741 new_bus = new_bus->parent; in vga_arbiter_check_bridge_sharing()
744 if (vgadev->bridge_has_one_vga) in vga_arbiter_check_bridge_sharing()
745 vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n"); in vga_arbiter_check_bridge_sharing()
747 vgaarb_info(&vgadev->pdev->dev, "no bridge control possible\n"); in vga_arbiter_check_bridge_sharing()
764 /* Only deal with VGA class devices */ in vga_arbiter_add_pci_device()
765 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) in vga_arbiter_add_pci_device()
771 vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n"); in vga_arbiter_add_pci_device()
785 vgadev->pdev = pdev; in vga_arbiter_add_pci_device()
788 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | in vga_arbiter_add_pci_device()
798 vgadev->owns |= VGA_RSRC_LEGACY_IO; in vga_arbiter_add_pci_device()
800 vgadev->owns |= VGA_RSRC_LEGACY_MEM; in vga_arbiter_add_pci_device()
802 /* Check if VGA cycles can get down to us */ in vga_arbiter_add_pci_device()
803 bus = pdev->bus; in vga_arbiter_add_pci_device()
805 bridge = bus->self; in vga_arbiter_add_pci_device()
811 vgadev->owns = 0; in vga_arbiter_add_pci_device()
815 bus = bus->parent; in vga_arbiter_add_pci_device()
819 vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n", in vga_arbiter_add_pci_device()
828 list_add_tail(&vgadev->list, &vga_list); in vga_arbiter_add_pci_device()
830 vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n", in vga_arbiter_add_pci_device()
831 vga_iostate_to_str(vgadev->decodes), in vga_arbiter_add_pci_device()
832 vga_iostate_to_str(vgadev->owns), in vga_arbiter_add_pci_device()
833 vga_iostate_to_str(vgadev->locks)); in vga_arbiter_add_pci_device()
859 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM)) in vga_arbiter_del_pci_device()
860 vga_decode_count--; in vga_arbiter_del_pci_device()
863 list_del(&vgadev->list); in vga_arbiter_del_pci_device()
864 vga_count--; in vga_arbiter_del_pci_device()
878 struct device *dev = &vgadev->pdev->dev; in vga_update_device_decodes()
881 old_decodes = vgadev->decodes; in vga_update_device_decodes()
883 decodes_unlocked = vgadev->locks & decodes_removed; in vga_update_device_decodes()
884 vgadev->decodes = new_decodes; in vga_update_device_decodes()
886 vgaarb_info(dev, "changed VGA decodes: olddecodes=%s,decodes=%s:owns=%s\n", in vga_update_device_decodes()
888 vga_iostate_to_str(vgadev->decodes), in vga_update_device_decodes()
889 vga_iostate_to_str(vgadev->owns)); in vga_update_device_decodes()
894 vgadev->io_lock_cnt = 0; in vga_update_device_decodes()
896 vgadev->mem_lock_cnt = 0; in vga_update_device_decodes()
903 vga_decode_count--; in vga_update_device_decodes()
925 if (userspace && vgadev->set_decode) in __vga_set_legacy_decoding()
933 * ship of non-legacy region ... in __vga_set_legacy_decoding()
941 * @pdev: pci device of the VGA card
944 * Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA
957 * vga_client_register - register or unregister a VGA arbitration client
958 * @pdev: pci device of the VGA client
959 * @set_decode: vga decode change callback
963 * @set_decode callback: If a client can disable its GPU VGA resource, it
966 * Rationale: we cannot disable VGA decode resources unconditionally some single
967 * GPU laptops seem to require ACPI or BIOS access to the VGA registers to
968 * control things like backlights etc. Hopefully newer multi-GPU laptops do
970 * driver will get a callback when VGA arbitration is first used by userspace
978 * Returns: 0 on success, -1 on failure
983 int ret = -ENODEV; in vga_client_register()
992 vgadev->set_decode = set_decode; in vga_client_register()
1008 * attached to the default VGA device of the system.
1019 * card_ID and an -ENODEV error is returned for any command
1028 * trylock <io_state> : non-blocking acquire locks on target
1050 * If the device is hot-unplugged, there is a hook inside the module to notify
1056 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
1079 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
1080 * returns the respective values. If the string is not in this format,
1102 struct vga_arb_private *priv = file->private_data; in vga_arb_read()
1112 return -ENOMEM; in vga_arb_read()
1118 pdev = priv->target; in vga_arb_read()
1140 vga_iostate_to_str(vgadev->decodes), in vga_arb_read()
1141 vga_iostate_to_str(vgadev->owns), in vga_arb_read()
1142 vga_iostate_to_str(vgadev->locks), in vga_arb_read()
1143 vgadev->io_lock_cnt, vgadev->mem_lock_cnt); in vga_arb_read()
1154 return -EFAULT; in vga_arb_read()
1165 struct vga_arb_private *priv = file->private_data; in vga_arb_write()
1178 return -EINVAL; in vga_arb_write()
1180 return -EFAULT; in vga_arb_write()
1186 remaining -= 5; in vga_arb_write()
1191 ret_val = -EPROTO; in vga_arb_write()
1195 ret_val = -EPROTO; in vga_arb_write()
1199 pdev = priv->target; in vga_arb_write()
1200 if (priv->target == NULL) { in vga_arb_write()
1201 ret_val = -ENODEV; in vga_arb_write()
1209 if (priv->cards[i].pdev == pdev) { in vga_arb_write()
1211 priv->cards[i].io_cnt++; in vga_arb_write()
1213 priv->cards[i].mem_cnt++; in vga_arb_write()
1222 remaining -= 7; in vga_arb_write()
1231 ret_val = -EPROTO; in vga_arb_write()
1236 ret_val = -EPROTO; in vga_arb_write()
1242 pdev = priv->target; in vga_arb_write()
1243 if (priv->target == NULL) { in vga_arb_write()
1244 ret_val = -ENODEV; in vga_arb_write()
1248 if (priv->cards[i].pdev == pdev) in vga_arb_write()
1249 uc = &priv->cards[i]; in vga_arb_write()
1253 ret_val = -EINVAL; in vga_arb_write()
1257 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) { in vga_arb_write()
1258 ret_val = -EINVAL; in vga_arb_write()
1262 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) { in vga_arb_write()
1263 ret_val = -EINVAL; in vga_arb_write()
1270 uc->io_cnt--; in vga_arb_write()
1272 uc->mem_cnt--; in vga_arb_write()
1278 remaining -= 8; in vga_arb_write()
1283 ret_val = -EPROTO; in vga_arb_write()
1288 ret_val = -EPROTO; in vga_arb_write()
1293 pdev = priv->target; in vga_arb_write()
1294 if (priv->target == NULL) { in vga_arb_write()
1295 ret_val = -ENODEV; in vga_arb_write()
1302 if (priv->cards[i].pdev == pdev) { in vga_arb_write()
1304 priv->cards[i].io_cnt++; in vga_arb_write()
1306 priv->cards[i].mem_cnt++; in vga_arb_write()
1313 ret_val = -EBUSY; in vga_arb_write()
1322 remaining -= 7; in vga_arb_write()
1330 ret_val = -EPROTO; in vga_arb_write()
1338 ret_val = -ENODEV; in vga_arb_write()
1351 vgaarb_dbg(&pdev->dev, "not a VGA device\n"); in vga_arb_write()
1355 ret_val = -ENODEV; in vga_arb_write()
1359 priv->target = pdev; in vga_arb_write()
1361 if (priv->cards[i].pdev == pdev) in vga_arb_write()
1363 if (priv->cards[i].pdev == NULL) { in vga_arb_write()
1364 priv->cards[i].pdev = pdev; in vga_arb_write()
1365 priv->cards[i].io_cnt = 0; in vga_arb_write()
1366 priv->cards[i].mem_cnt = 0; in vga_arb_write()
1371 vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n", in vga_arb_write()
1375 ret_val = -ENOMEM; in vga_arb_write()
1386 remaining -= 8; in vga_arb_write()
1390 ret_val = -EPROTO; in vga_arb_write()
1393 pdev = priv->target; in vga_arb_write()
1394 if (priv->target == NULL) { in vga_arb_write()
1395 ret_val = -ENODEV; in vga_arb_write()
1404 return -EPROTO; in vga_arb_write()
1427 return -ENOMEM; in vga_arb_open()
1428 spin_lock_init(&priv->lock); in vga_arb_open()
1429 file->private_data = priv; in vga_arb_open()
1432 list_add(&priv->list, &vga_user_list); in vga_arb_open()
1436 priv->target = vga_default_device(); /* Maybe this is still null! */ in vga_arb_open()
1437 priv->cards[0].pdev = priv->target; in vga_arb_open()
1438 priv->cards[0].io_cnt = 0; in vga_arb_open()
1439 priv->cards[0].mem_cnt = 0; in vga_arb_open()
1447 struct vga_arb_private *priv = file->private_data; in vga_arb_release()
1455 list_del(&priv->list); in vga_arb_release()
1457 uc = &priv->cards[i]; in vga_arb_release()
1458 if (uc->pdev == NULL) in vga_arb_release()
1460 vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n", in vga_arb_release()
1461 uc->io_cnt, uc->mem_cnt); in vga_arb_release()
1462 while (uc->io_cnt--) in vga_arb_release()
1463 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO); in vga_arb_release()
1464 while (uc->mem_cnt--) in vga_arb_release()
1465 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM); in vga_arb_release()
1476 * change in VGA cards
1494 if (vgadev->set_decode) { in vga_arbiter_notify_clients()
1495 new_decodes = vgadev->set_decode(vgadev->pdev, in vga_arbiter_notify_clients()
1514 * cases of hotplugable vga cards. */ in pci_notify()
1553 /* We add all PCI devices satisfying VGA class in the arbiter by in vga_arb_device_init()