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()
767 /* Only deal with VGA class devices */ in vga_arbiter_add_pci_device()
768 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) in vga_arbiter_add_pci_device()
774 vgaarb_err(&pdev->dev, "failed to allocate VGA arbiter data\n"); in vga_arbiter_add_pci_device()
788 vgadev->pdev = pdev; in vga_arbiter_add_pci_device()
791 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | in vga_arbiter_add_pci_device()
803 vgadev->owns |= VGA_RSRC_LEGACY_IO; in vga_arbiter_add_pci_device()
805 vgadev->owns |= VGA_RSRC_LEGACY_MEM; in vga_arbiter_add_pci_device()
807 /* Check if VGA cycles can get down to us */ in vga_arbiter_add_pci_device()
808 bus = pdev->bus; in vga_arbiter_add_pci_device()
810 bridge = bus->self; in vga_arbiter_add_pci_device()
816 vgadev->owns = 0; in vga_arbiter_add_pci_device()
820 bus = bus->parent; in vga_arbiter_add_pci_device()
824 vgaarb_info(&pdev->dev, "setting as boot VGA device%s\n", in vga_arbiter_add_pci_device()
833 list_add_tail(&vgadev->list, &vga_list); in vga_arbiter_add_pci_device()
835 vgaarb_info(&pdev->dev, "VGA device added: decodes=%s,owns=%s,locks=%s\n", in vga_arbiter_add_pci_device()
836 vga_iostate_to_str(vgadev->decodes), in vga_arbiter_add_pci_device()
837 vga_iostate_to_str(vgadev->owns), in vga_arbiter_add_pci_device()
838 vga_iostate_to_str(vgadev->locks)); in vga_arbiter_add_pci_device()
864 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM)) in vga_arbiter_del_pci_device()
865 vga_decode_count--; in vga_arbiter_del_pci_device()
868 list_del(&vgadev->list); in vga_arbiter_del_pci_device()
869 vga_count--; in vga_arbiter_del_pci_device()
883 struct device *dev = &vgadev->pdev->dev; in vga_update_device_decodes()
884 unsigned int old_decodes = vgadev->decodes; in vga_update_device_decodes()
886 unsigned int decodes_unlocked = vgadev->locks & decodes_removed; in vga_update_device_decodes()
888 vgadev->decodes = new_decodes; in vga_update_device_decodes()
890 vgaarb_info(dev, "VGA decodes changed: olddecodes=%s,decodes=%s:owns=%s\n", in vga_update_device_decodes()
892 vga_iostate_to_str(vgadev->decodes), in vga_update_device_decodes()
893 vga_iostate_to_str(vgadev->owns)); in vga_update_device_decodes()
898 vgadev->io_lock_cnt = 0; in vga_update_device_decodes()
900 vgadev->mem_lock_cnt = 0; in vga_update_device_decodes()
907 vga_decode_count--; in vga_update_device_decodes()
929 if (userspace && vgadev->set_decode) in __vga_set_legacy_decoding()
938 * ownership of non-legacy region. in __vga_set_legacy_decoding()
946 * @pdev: PCI device of the VGA card
949 * Indicate to the arbiter if the card decodes legacy VGA IOs, legacy VGA
962 * vga_client_register - register or unregister a VGA arbitration client
963 * @pdev: PCI device of the VGA client
964 * @set_decode: VGA decode change callback
968 * @set_decode callback: If a client can disable its GPU VGA resource, it
971 * Rationale: we cannot disable VGA decode resources unconditionally
973 * the VGA registers to control things like backlights etc. Hopefully newer
974 * multi-GPU laptops do something saner, and desktops won't have any
975 * special ACPI for this. The driver will get a callback when VGA
983 * Returns: 0 on success, -ENODEV on failure
994 vgadev->set_decode = set_decode; in vga_client_register()
997 return -ENODEV; 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)
1077 * Take a string in the format: "PCI:domain:bus:dev.fn" and return the
1078 * respective values. If the string is not in this format, return 0.
1098 struct vga_arb_private *priv = file->private_data; in vga_arb_read()
1108 return -ENOMEM; in vga_arb_read()
1114 pdev = priv->target; in vga_arb_read()
1137 vga_iostate_to_str(vgadev->decodes), in vga_arb_read()
1138 vga_iostate_to_str(vgadev->owns), in vga_arb_read()
1139 vga_iostate_to_str(vgadev->locks), in vga_arb_read()
1140 vgadev->io_lock_cnt, vgadev->mem_lock_cnt); in vga_arb_read()
1151 return -EFAULT; in vga_arb_read()
1162 struct vga_arb_private *priv = file->private_data; in vga_arb_write()
1175 return -EINVAL; in vga_arb_write()
1177 return -EFAULT; in vga_arb_write()
1183 remaining -= 5; in vga_arb_write()
1188 ret_val = -EPROTO; in vga_arb_write()
1192 ret_val = -EPROTO; in vga_arb_write()
1196 pdev = priv->target; in vga_arb_write()
1197 if (priv->target == NULL) { in vga_arb_write()
1198 ret_val = -ENODEV; in vga_arb_write()
1206 if (priv->cards[i].pdev == pdev) { in vga_arb_write()
1208 priv->cards[i].io_cnt++; in vga_arb_write()
1210 priv->cards[i].mem_cnt++; in vga_arb_write()
1219 remaining -= 7; in vga_arb_write()
1228 ret_val = -EPROTO; in vga_arb_write()
1233 ret_val = -EPROTO; in vga_arb_write()
1239 pdev = priv->target; in vga_arb_write()
1240 if (priv->target == NULL) { in vga_arb_write()
1241 ret_val = -ENODEV; in vga_arb_write()
1245 if (priv->cards[i].pdev == pdev) in vga_arb_write()
1246 uc = &priv->cards[i]; in vga_arb_write()
1250 ret_val = -EINVAL; in vga_arb_write()
1254 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) { in vga_arb_write()
1255 ret_val = -EINVAL; in vga_arb_write()
1259 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) { in vga_arb_write()
1260 ret_val = -EINVAL; in vga_arb_write()
1267 uc->io_cnt--; in vga_arb_write()
1269 uc->mem_cnt--; in vga_arb_write()
1275 remaining -= 8; in vga_arb_write()
1280 ret_val = -EPROTO; in vga_arb_write()
1285 ret_val = -EPROTO; in vga_arb_write()
1290 pdev = priv->target; in vga_arb_write()
1291 if (priv->target == NULL) { in vga_arb_write()
1292 ret_val = -ENODEV; in vga_arb_write()
1299 if (priv->cards[i].pdev == pdev) { in vga_arb_write()
1301 priv->cards[i].io_cnt++; in vga_arb_write()
1303 priv->cards[i].mem_cnt++; in vga_arb_write()
1310 ret_val = -EBUSY; in vga_arb_write()
1319 remaining -= 7; in vga_arb_write()
1327 ret_val = -EPROTO; in vga_arb_write()
1335 ret_val = -ENODEV; in vga_arb_write()
1348 vgaarb_dbg(&pdev->dev, "not a VGA device\n"); in vga_arb_write()
1352 ret_val = -ENODEV; in vga_arb_write()
1356 priv->target = pdev; in vga_arb_write()
1358 if (priv->cards[i].pdev == pdev) in vga_arb_write()
1360 if (priv->cards[i].pdev == NULL) { in vga_arb_write()
1361 priv->cards[i].pdev = pdev; in vga_arb_write()
1362 priv->cards[i].io_cnt = 0; in vga_arb_write()
1363 priv->cards[i].mem_cnt = 0; in vga_arb_write()
1368 vgaarb_dbg(&pdev->dev, "maximum user cards (%d) number reached, ignoring this one!\n", in vga_arb_write()
1372 ret_val = -ENOMEM; in vga_arb_write()
1383 remaining -= 8; in vga_arb_write()
1387 ret_val = -EPROTO; in vga_arb_write()
1390 pdev = priv->target; in vga_arb_write()
1391 if (priv->target == NULL) { in vga_arb_write()
1392 ret_val = -ENODEV; in vga_arb_write()
1401 return -EPROTO; in vga_arb_write()
1424 return -ENOMEM; in vga_arb_open()
1425 spin_lock_init(&priv->lock); in vga_arb_open()
1426 file->private_data = priv; in vga_arb_open()
1429 list_add(&priv->list, &vga_user_list); in vga_arb_open()
1433 priv->target = vga_default_device(); /* Maybe this is still null! */ in vga_arb_open()
1434 priv->cards[0].pdev = priv->target; in vga_arb_open()
1435 priv->cards[0].io_cnt = 0; in vga_arb_open()
1436 priv->cards[0].mem_cnt = 0; in vga_arb_open()
1443 struct vga_arb_private *priv = file->private_data; in vga_arb_release()
1451 list_del(&priv->list); in vga_arb_release()
1453 uc = &priv->cards[i]; in vga_arb_release()
1454 if (uc->pdev == NULL) in vga_arb_release()
1456 vgaarb_dbg(&uc->pdev->dev, "uc->io_cnt == %d, uc->mem_cnt == %d\n", in vga_arb_release()
1457 uc->io_cnt, uc->mem_cnt); in vga_arb_release()
1458 while (uc->io_cnt--) in vga_arb_release()
1459 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO); in vga_arb_release()
1460 while (uc->mem_cnt--) in vga_arb_release()
1461 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM); in vga_arb_release()
1471 * Callback any registered clients to let them know we have a change in VGA
1488 if (vgadev->set_decode) { in vga_arbiter_notify_clients()
1489 new_decodes = vgadev->set_decode(vgadev->pdev, in vga_arbiter_notify_clients()
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()