Lines Matching +full:vga +full:- +full:format
1 // SPDX-License-Identifier: MIT
3 * vgaarb.c: Implements VGA arbitration. For details refer to
39 * We keep a list of all VGA devices in the system to speed
111 if (pdev == vgadev->pdev) in vgadev_find()
117 * vga_default_device - return the default VGA device, for vgacon
120 * rather dumb and will probably only work properly on single VGA card
123 * If your VGA default device is not PCI, you'll have to return NULL here.
126 * the VGA default device if that is possible. This may be a problem with
127 * real _ISA_ VGA cards, in addition to a PCI one. I don't know at this
148 * vga_remove_vgacon - deactivate VGA console
150 * Unbind and unregister vgacon in case pdev is the default VGA device.
151 * Can be called by GPU drivers on initialization to make sure VGA register
164 return -ENODEV; in vga_remove_vgacon()
173 vgaarb_info(&pdev->dev, "deactivate vga console\n"); in vga_remove_vgacon()
178 MAX_NR_CONSOLES - 1, 1); in vga_remove_vgacon()
183 if (ret == -ENODEV) in vga_remove_vgacon()
194 * If we don't ever use VGA arbitration, we should avoid turning off
196 * device not being VGA.
201 * Inform all GPUs in the system that VGA arbitration has occurred in vga_check_first_use()
213 struct device *dev = &vgadev->pdev->dev; in __vga_tryget()
224 (vgadev->decodes & VGA_RSRC_LEGACY_IO)) in __vga_tryget()
227 (vgadev->decodes & VGA_RSRC_LEGACY_MEM)) in __vga_tryget()
231 vgaarb_dbg(dev, "%s: owns: %d\n", __func__, vgadev->owns); in __vga_tryget()
234 wants = rsrc & ~vgadev->owns; in __vga_tryget()
262 * VGA forwarding on P2P bridges. in __vga_tryget()
264 if (vgadev->pdev->bus != conflict->pdev->bus) { in __vga_tryget()
273 if (conflict->locks & lwants) in __vga_tryget()
281 match = lwants & conflict->owns; in __vga_tryget()
297 if (!conflict->bridge_has_one_vga) { in __vga_tryget()
298 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_MEM) in __vga_tryget()
300 if ((match & conflict->decodes) & VGA_RSRC_LEGACY_IO) in __vga_tryget()
310 pci_set_vga_state(conflict->pdev, false, pci_bits, flags); in __vga_tryget()
311 conflict->owns &= ~match; in __vga_tryget()
315 conflict->owns &= ~VGA_RSRC_NORMAL_MEM; in __vga_tryget()
317 conflict->owns &= ~VGA_RSRC_NORMAL_IO; in __vga_tryget()
330 if (!vgadev->bridge_has_one_vga) { in __vga_tryget()
340 pci_set_vga_state(vgadev->pdev, true, pci_bits, flags); in __vga_tryget()
342 vgadev->owns |= wants; in __vga_tryget()
344 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK); in __vga_tryget()
346 vgadev->io_lock_cnt++; in __vga_tryget()
348 vgadev->mem_lock_cnt++; in __vga_tryget()
350 vgadev->io_norm_cnt++; in __vga_tryget()
352 vgadev->mem_norm_cnt++; in __vga_tryget()
359 struct device *dev = &vgadev->pdev->dev; in __vga_put()
360 unsigned int old_locks = vgadev->locks; in __vga_put()
368 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) { in __vga_put()
369 vgadev->io_norm_cnt--; in __vga_put()
370 if (vgadev->decodes & VGA_RSRC_LEGACY_IO) in __vga_put()
373 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) { in __vga_put()
374 vgadev->mem_norm_cnt--; in __vga_put()
375 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM) in __vga_put()
378 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0) in __vga_put()
379 vgadev->io_lock_cnt--; in __vga_put()
380 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0) in __vga_put()
381 vgadev->mem_lock_cnt--; in __vga_put()
387 if (vgadev->io_lock_cnt == 0) in __vga_put()
388 vgadev->locks &= ~VGA_RSRC_LEGACY_IO; in __vga_put()
389 if (vgadev->mem_lock_cnt == 0) in __vga_put()
390 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM; in __vga_put()
396 if (old_locks != vgadev->locks) in __vga_put()
401 * vga_get - acquire & lock VGA resources
402 * @pdev: PCI device of the VGA card or NULL for the system default
406 * Acquire VGA resources for the given card and mark those resources
412 * The arbiter will first look for all VGA cards that might conflict and disable
413 * their IOs and/or Memory access, including VGA forwarding on P2P bridges if
416 * enabled on the card (including VGA forwarding on parent P2P bridges if any).
420 * bridges don't differentiate VGA memory and IO afaik). You can indicate
426 * supported (a per-resource counter is maintained)
428 * On success, release the VGA resource again with vga_put().
453 rc = -ENODEV; in vga_get()
465 * be fairly easy to have a per-device one so that we only in vga_get()
476 rc = -ERESTARTSYS; in vga_get()
487 * vga_tryget - try to acquire & lock legacy VGA resources
488 * @pdev: PCI device of VGA card or NULL for system default
491 * Perform the same operation as vga_get(), but return an error (-EBUSY)
495 * On success, release the VGA resource again with vga_put().
517 rc = -ENODEV; in vga_tryget()
521 rc = -EBUSY; in vga_tryget()
528 * vga_put - release lock on legacy VGA resources
529 * @pdev: PCI device of VGA card or NULL for system default
577 if (!r->start || !r->end) in vga_is_firmware_default()
580 if (base < r->start || limit >= r->end) in vga_is_firmware_default()
601 * Return true if vgadev is a better default VGA device than the best one
607 struct pci_dev *pdev = vgadev->pdev; in vga_is_boot_device()
611 * We select the default VGA device in this order: in vga_is_boot_device()
613 * Legacy VGA device (owns VGA_RSRC_LEGACY_MASK) in vga_is_boot_device()
614 * Non-legacy integrated device (see vga_arb_select_default_device()) in vga_is_boot_device()
615 * Non-legacy discrete device (see vga_arb_select_default_device()) in vga_is_boot_device()
623 if (boot_vga && boot_vga->is_firmware_default) in vga_is_boot_device()
627 vgadev->is_firmware_default = true; in vga_is_boot_device()
632 * A legacy VGA device has MEM and IO enabled and any bridges in vga_is_boot_device()
634 * resources ([mem 0xa0000-0xbffff], [io 0x3b0-0x3bb], etc) are in vga_is_boot_device()
641 (boot_vga->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK) in vga_is_boot_device()
644 if ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK) in vga_is_boot_device()
648 * If we haven't found a legacy VGA device, accept a non-legacy in vga_is_boot_device()
651 * use legacy VGA resources. Prefer an integrated GPU over others. in vga_is_boot_device()
657 * An integrated GPU overrides a previous non-legacy in vga_is_boot_device()
662 if (vga_arb_integrated_gpu(&pdev->dev)) in vga_is_boot_device()
666 * We prefer the first non-legacy discrete device we find. in vga_is_boot_device()
670 pci_read_config_word(boot_vga->pdev, PCI_COMMAND, in vga_is_boot_device()
680 * other VGA devices, it is the best candidate so far. in vga_is_boot_device()
689 * Rules for using a bridge to control a VGA descendant decoding: if a bridge
690 * has only one VGA descendant then it can be used to control the VGA routing
692 * control it. If a bridge has a direct VGA descendant, but also have a sub-
693 * bridge VGA descendant then we cannot use that bridge to control the direct
694 * VGA descendant. So for every device we register, we need to iterate all
703 vgadev->bridge_has_one_vga = true; in vga_arbiter_check_bridge_sharing()
706 vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n"); in vga_arbiter_check_bridge_sharing()
711 new_bus = vgadev->pdev->bus; in vga_arbiter_check_bridge_sharing()
713 new_bridge = new_bus->self; in vga_arbiter_check_bridge_sharing()
717 bus = same_bridge_vgadev->pdev->bus; in vga_arbiter_check_bridge_sharing()
718 bridge = bus->self; in vga_arbiter_check_bridge_sharing()
727 same_bridge_vgadev->bridge_has_one_vga = false; in vga_arbiter_check_bridge_sharing()
737 bridge = bus->self; in vga_arbiter_check_bridge_sharing()
739 if (bridge && bridge == vgadev->pdev->bus->self) in vga_arbiter_check_bridge_sharing()
740 vgadev->bridge_has_one_vga = false; in vga_arbiter_check_bridge_sharing()
742 bus = bus->parent; in vga_arbiter_check_bridge_sharing()
745 new_bus = new_bus->parent; in vga_arbiter_check_bridge_sharing()
748 if (vgadev->bridge_has_one_vga) in vga_arbiter_check_bridge_sharing()
749 vgaarb_info(&vgadev->pdev->dev, "bridge control possible\n"); in vga_arbiter_check_bridge_sharing()
751 vgaarb_info(&vgadev->pdev->dev, "no bridge control possible\n"); in vga_arbiter_check_bridge_sharing()
770 vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n"); in vga_arbiter_add_pci_device()
784 vgadev->pdev = pdev; in vga_arbiter_add_pci_device()
787 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | in vga_arbiter_add_pci_device()
799 vgadev->owns |= VGA_RSRC_LEGACY_IO; in vga_arbiter_add_pci_device()
801 vgadev->owns |= VGA_RSRC_LEGACY_MEM; in vga_arbiter_add_pci_device()
803 /* Check if VGA cycles can get down to us */ in vga_arbiter_add_pci_device()
804 bus = pdev->bus; in vga_arbiter_add_pci_device()
806 bridge = bus->self; in vga_arbiter_add_pci_device()
812 vgadev->owns = 0; in vga_arbiter_add_pci_device()
816 bus = bus->parent; in vga_arbiter_add_pci_device()
820 vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n", in vga_arbiter_add_pci_device()
829 list_add_tail(&vgadev->list, &vga_list); in vga_arbiter_add_pci_device()
831 vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n", in vga_arbiter_add_pci_device()
832 vga_iostate_to_str(vgadev->decodes), in vga_arbiter_add_pci_device()
833 vga_iostate_to_str(vgadev->owns), in vga_arbiter_add_pci_device()
834 vga_iostate_to_str(vgadev->locks)); in vga_arbiter_add_pci_device()
860 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM)) in vga_arbiter_del_pci_device()
861 vga_decode_count--; in vga_arbiter_del_pci_device()
864 list_del(&vgadev->list); in vga_arbiter_del_pci_device()
865 vga_count--; in vga_arbiter_del_pci_device()
879 struct device *dev = &vgadev->pdev->dev; in vga_update_device_decodes()
880 unsigned int old_decodes = vgadev->decodes; in vga_update_device_decodes()
882 unsigned int 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, "VGA decodes changed: 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()
934 * ownership of non-legacy region. in __vga_set_legacy_decoding()
942 * @pdev: PCI device of the VGA card
945 * Indicate to the arbiter if the card decodes legacy VGA IOs, legacy VGA
958 * vga_client_register - register or unregister a VGA arbitration client
959 * @pdev: PCI device of the VGA client
960 * @set_decode: VGA decode change callback
964 * @set_decode callback: If a client can disable its GPU VGA resource, it
967 * Rationale: we cannot disable VGA decode resources unconditionally
969 * the VGA registers to control things like backlights etc. Hopefully newer
970 * multi-GPU laptops do something saner, and desktops won't have any
971 * special ACPI for this. The driver will get a callback when VGA
979 * Returns: 0 on success, -ENODEV on failure
990 vgadev->set_decode = set_decode; in vga_client_register()
993 return -ENODEV; in vga_client_register()
1004 * attached to the default VGA device of the system.
1015 * card_ID and an -ENODEV error is returned for any command
1024 * trylock <io_state> : non-blocking acquire locks on target
1046 * If the device is hot-unplugged, there is a hook inside the module to notify
1052 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
1073 * Take a string in the format: "PCI:domain:bus:dev.fn" and return the
1074 * respective values. If the string is not in this format, return 0.
1094 struct vga_arb_private *priv = file->private_data; in vga_arb_read()
1104 return -ENOMEM; in vga_arb_read()
1110 pdev = priv->target; in vga_arb_read()
1133 vga_iostate_to_str(vgadev->decodes), in vga_arb_read()
1134 vga_iostate_to_str(vgadev->owns), in vga_arb_read()
1135 vga_iostate_to_str(vgadev->locks), in vga_arb_read()
1136 vgadev->io_lock_cnt, vgadev->mem_lock_cnt); in vga_arb_read()
1147 return -EFAULT; in vga_arb_read()
1158 struct vga_arb_private *priv = file->private_data; in vga_arb_write()
1171 return -EINVAL; in vga_arb_write()
1173 return -EFAULT; in vga_arb_write()
1179 remaining -= 5; in vga_arb_write()
1184 ret_val = -EPROTO; in vga_arb_write()
1188 ret_val = -EPROTO; in vga_arb_write()
1192 pdev = priv->target; in vga_arb_write()
1193 if (priv->target == NULL) { in vga_arb_write()
1194 ret_val = -ENODEV; in vga_arb_write()
1202 if (priv->cards[i].pdev == pdev) { in vga_arb_write()
1204 priv->cards[i].io_cnt++; in vga_arb_write()
1206 priv->cards[i].mem_cnt++; in vga_arb_write()
1215 remaining -= 7; in vga_arb_write()
1224 ret_val = -EPROTO; in vga_arb_write()
1229 ret_val = -EPROTO; in vga_arb_write()
1235 pdev = priv->target; in vga_arb_write()
1236 if (priv->target == NULL) { in vga_arb_write()
1237 ret_val = -ENODEV; in vga_arb_write()
1241 if (priv->cards[i].pdev == pdev) in vga_arb_write()
1242 uc = &priv->cards[i]; in vga_arb_write()
1246 ret_val = -EINVAL; in vga_arb_write()
1250 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) { in vga_arb_write()
1251 ret_val = -EINVAL; in vga_arb_write()
1255 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) { in vga_arb_write()
1256 ret_val = -EINVAL; in vga_arb_write()
1263 uc->io_cnt--; in vga_arb_write()
1265 uc->mem_cnt--; in vga_arb_write()
1271 remaining -= 8; in vga_arb_write()
1276 ret_val = -EPROTO; in vga_arb_write()
1281 ret_val = -EPROTO; in vga_arb_write()
1286 pdev = priv->target; in vga_arb_write()
1287 if (priv->target == NULL) { in vga_arb_write()
1288 ret_val = -ENODEV; in vga_arb_write()
1295 if (priv->cards[i].pdev == pdev) { in vga_arb_write()
1297 priv->cards[i].io_cnt++; in vga_arb_write()
1299 priv->cards[i].mem_cnt++; in vga_arb_write()
1306 ret_val = -EBUSY; in vga_arb_write()
1315 remaining -= 7; in vga_arb_write()
1323 ret_val = -EPROTO; in vga_arb_write()
1331 ret_val = -ENODEV; in vga_arb_write()
1344 vgaarb_dbg(&pdev->dev, "not a VGA device\n"); in vga_arb_write()
1348 ret_val = -ENODEV; in vga_arb_write()
1352 priv->target = pdev; in vga_arb_write()
1354 if (priv->cards[i].pdev == pdev) in vga_arb_write()
1356 if (priv->cards[i].pdev == NULL) { in vga_arb_write()
1357 priv->cards[i].pdev = pdev; in vga_arb_write()
1358 priv->cards[i].io_cnt = 0; in vga_arb_write()
1359 priv->cards[i].mem_cnt = 0; in vga_arb_write()
1364 vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n", in vga_arb_write()
1368 ret_val = -ENOMEM; in vga_arb_write()
1379 remaining -= 8; in vga_arb_write()
1383 ret_val = -EPROTO; in vga_arb_write()
1386 pdev = priv->target; in vga_arb_write()
1387 if (priv->target == NULL) { in vga_arb_write()
1388 ret_val = -ENODEV; in vga_arb_write()
1397 return -EPROTO; in vga_arb_write()
1420 return -ENOMEM; in vga_arb_open()
1421 spin_lock_init(&priv->lock); in vga_arb_open()
1422 file->private_data = priv; in vga_arb_open()
1425 list_add(&priv->list, &vga_user_list); in vga_arb_open()
1429 priv->target = vga_default_device(); /* Maybe this is still null! */ in vga_arb_open()
1430 priv->cards[0].pdev = priv->target; in vga_arb_open()
1431 priv->cards[0].io_cnt = 0; in vga_arb_open()
1432 priv->cards[0].mem_cnt = 0; in vga_arb_open()
1439 struct vga_arb_private *priv = file->private_data; in vga_arb_release()
1447 list_del(&priv->list); in vga_arb_release()
1449 uc = &priv->cards[i]; in vga_arb_release()
1450 if (uc->pdev == NULL) in vga_arb_release()
1452 vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n", in vga_arb_release()
1453 uc->io_cnt, uc->mem_cnt); in vga_arb_release()
1454 while (uc->io_cnt--) in vga_arb_release()
1455 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO); in vga_arb_release()
1456 while (uc->mem_cnt--) in vga_arb_release()
1457 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM); in vga_arb_release()
1467 * Callback any registered clients to let them know we have a change in VGA
1484 if (vgadev->set_decode) { in vga_arbiter_notify_clients()
1485 new_decodes = vgadev->set_decode(vgadev->pdev, in vga_arbiter_notify_clients()
1502 /* Only deal with VGA class devices */ in pci_notify()
1509 * for the cases of hot-pluggable VGA cards. in pci_notify()
1549 /* Add all VGA class PCI devices by default */ in vga_arb_device_init()