vga: implements VGA arbitration on Linux
Subsystems (7)
Dave Airlie
maintainerPATCH 1/1 drm: add support for VGA arbitration
In reply to Dave AirlieFrom: Dave Airlie <[email protected]>
this patch has two components:
a) for legacy DRM drivers, we need a hook to enable/disable irqs around arb when the command/memory is turned off.
b) for KMS drivers we provide a hook/callback to enable/disable VGA decoding.
We don't disable VGA decoding in the single device case, since we believe some laptops ACPI implementation will react badly.
--- drivers/gpu/drm/drm_irq.c | 27 +++++++++++++++++++++++++++ drivers/gpu/drm/i915/i915_dma.c | 20 ++++++++++++++++++++ drivers/gpu/drm/i915/i915_drv.h | 1 + drivers/gpu/drm/i915/i915_reg.h | 1 + drivers/gpu/drm/i915/intel_display.c | 23 +++++++++++++++++++++++ drivers/gpu/drm/i915/intel_drv.h | 1 + drivers/gpu/drm/radeon/r100.c | 14 ++++++++++++++ drivers/gpu/drm/radeon/radeon.h | 2 ++ drivers/gpu/drm/radeon/radeon_asic.h | 9 +++++++++ drivers/gpu/drm/radeon/radeon_device.c | 18 ++++++++++++++++++ include/drm/drmP.h | 3 +++ 11 files changed, 119 insertions(+), 0 deletions(-)
drivers/gpu/drm/drm_irq.c +27 -0
@@ -37,6 +37,7 @@#include <linux/interrupt.h> /* For task queue support */ +#include <linux/vgaarb.h> /** * Get interrupt from bus id. *
@@ -171,6 +172,26 @@ err: }EXPORT_SYMBOL (drm_vblank_init); +static void drm_irq_vgaarb_nokms (void *cookie,bool state) +{ +struct drm_device *dev = cookie; + +if (dev->driver->vgaarb_irq) { + dev->driver->vgaarb_irq (dev, state); +return ; + } + +if (!dev->irq_enabled) +return ; + +if (state) + dev->driver->irq_uninstall (dev); +else { + dev->driver->irq_preinstall (dev); + dev->driver->irq_postinstall (dev); + } +} +/** * Install IRQ handler. *
@@ -231,6 +252,9 @@ int drm_irq_install(struct drm_device *dev)return ret; } +if (!drm_core_check_feature (dev,DRIVER_MODESET )) +vga_client_register (dev->pdev, (void *)dev, drm_irq_vgaarb_nokms,NULL ); +/* After installing handler */ ret = dev->driver->irq_postinstall (dev);if (ret <0 ) {
@@ -279,6 +303,9 @@ int drm_irq_uninstall(struct drm_device * dev)DRM_DEBUG ("irq=%d\n" , dev->pdev->irq); +if (!drm_core_check_feature (dev,DRIVER_MODESET )) +vga_client_register (dev->pdev,NULL ,NULL ,NULL ); + dev->driver->irq_uninstall (dev);free_irq (dev->pdev->irq, dev);
drivers/gpu/drm/i915/i915_dma.c +20 -0
@@ -33,6 +33,7 @@#include "i915_drm.h" #include "i915_drv.h" +#include <linux/vgaarb.h> #define I915_DRV "i915_drv"/* Really want an OS-independent resettable timer. Would like to have
@@ -984,6 +985,19 @@ static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,return 0 ; } +/* true = enable decode, false = disable decoder */ +static unsigned int i915_vga_set_decode (void *cookie,bool state) +{ +struct drm_device *dev = cookie; + +intel_modeset_vga_set_state (dev, state); +if (state) +return VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM | +VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; +else +return VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; +} +static int i915_load_modeset_init (struct drm_device *dev,unsigned long prealloc_size,unsigned long agp_size)
@@ -1029,6 +1043,11 @@ static int i915_load_modeset_init(struct drm_device *dev,if (ret)DRM_INFO ("failed to find VBIOS tables\n" ); +/* if we have > 1 VGA cards, then disable the radeon VGA resources */ + ret =vga_client_register (dev->pdev, dev,NULL , i915_vga_set_decode); +if (ret) + goto destroy_ringbuffer; + ret =drm_irq_install (dev);if (ret) goto destroy_ringbuffer;
@@ -1289,6 +1308,7 @@ int i915_driver_unload(struct drm_device *dev)if (drm_core_check_feature (dev,DRIVER_MODESET )) {drm_irq_uninstall (dev); +vga_client_register (dev->pdev,NULL ,NULL ,NULL ); }if (dev->pdev->msi_enabled)
drivers/gpu/drm/i915/i915_drv.h +1 -0
@@ -750,6 +750,7 @@ static inline void opregion_enable_asle(struct drm_device *dev) { return; }/* modesetting */ extern void intel_modeset_init (struct drm_device *dev);extern void intel_modeset_cleanup (struct drm_device *dev); +extern int intel_modeset_vga_set_state (struct drm_device *dev,bool state);/** * Lock test for when it's just for synchronization of ring access.
drivers/gpu/drm/i915/i915_reg.h +1 -0
@@ -30,6 +30,7 @@* fb aperture size and the amount of pre-reserved memory. */ #define INTEL_GMCH_CTRL 0x52 +#define INTEL_GMCH_VGA_DISABLE (1 << 1)#define INTEL_GMCH_ENABLED 0x4#define INTEL_GMCH_MEM_MASK 0x1#define INTEL_GMCH_MEM_64M 0x1
drivers/gpu/drm/i915/intel_display.c +23 -0
@@ -3447,3 +3447,26 @@ struct drm_encoder *intel_best_encoder(struct drm_connector *connector)return &intel_output->enc; } + +/* +* set vga decode state - true == enable VGA decode +*/ +int intel_modeset_vga_set_state (struct drm_device *dev,bool state) +{ +struct pci_dev *bridge_dev; +u16 gmch_ctrl; + + bridge_dev =pci_get_bus_and_slot (0 ,PCI_DEVFN (0 ,0 )); +if (!bridge_dev) { +DRM_ERROR ("Can't disable VGA, no bridge\n" ); +return -1 ; + } + +pci_read_config_word (bridge_dev,INTEL_GMCH_CTRL , &gmch_ctrl); +if (state) + gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE ; +else + gmch_ctrl |=INTEL_GMCH_VGA_DISABLE ; +pci_write_config_word (bridge_dev,INTEL_GMCH_CTRL , gmch_ctrl); +return 0 ; +}
drivers/gpu/drm/i915/intel_drv.h +1 -0
@@ -157,4 +157,5 @@ extern int intel_framebuffer_create(struct drm_device *dev,struct drm_mode_fb_cmd *mode_cmd, struct drm_framebuffer **fb, struct drm_gem_object *obj); +#endif /* __INTEL_DRV_H__ */
drivers/gpu/drm/radeon/r100.c +14 -0
@@ -1492,6 +1492,20 @@ void r100_vram_init_sizes(struct radeon_device *rdev) rdev->mc.real_vram_size = rdev->mc.aper_size; } +void r100_vga_set_state (struct radeon_device *rdev,bool state) +{ +uint32_t temp; + + temp =RREG32 (RADEON_CONFIG_CNTL ); +if (state == false) { + temp &= ~(1 <<8 ); + temp |= (1 <<9 ); + }else { + temp &= ~(1 <<9 ); + } +WREG32 (RADEON_CONFIG_CNTL , temp); +} +void r100_vram_info (struct radeon_device *rdev) {r100_vram_get_type (rdev);
drivers/gpu/drm/radeon/radeon.h +2 -0
@@ -559,6 +559,7 @@ struct radeon_asic {int (*init)(struct radeon_device *rdev);void (*errata)(struct radeon_device *rdev);void (*vram_info)(struct radeon_device *rdev); +void (*vga_set_state)(struct radeon_device *rdev,bool state);int (*gpu_reset)(struct radeon_device *rdev);int (*mc_init)(struct radeon_device *rdev);void (*mc_fini)(struct radeon_device *rdev);
@@ -847,6 +848,7 @@ static inline void radeon_ring_write(struct radeon_device *rdev, uint32_t v)#define radeon_cs_parse (p) rdev->asic->cs_parse((p))#define radeon_errata (rdev) (rdev)->asic->errata((rdev))#define radeon_vram_info (rdev) (rdev)->asic->vram_info((rdev)) +#define radeon_vga_set_state (rdev, state) (rdev)->asic->vga_set_state((rdev), (state))#define radeon_gpu_reset (rdev) (rdev)->asic->gpu_reset((rdev))#define radeon_mc_init (rdev) (rdev)->asic->mc_init((rdev))#define radeon_mc_fini (rdev) (rdev)->asic->mc_fini((rdev))
drivers/gpu/drm/radeon/radeon_asic.h +9 -0
@@ -46,6 +46,7 @@ uint32_t r100_mm_rreg(struct radeon_device *rdev, uint32_t reg);void r100_mm_wreg (struct radeon_device *rdev,uint32_t reg,uint32_t v);void r100_errata (struct radeon_device *rdev);void r100_vram_info (struct radeon_device *rdev); +void r100_vga_set_state (struct radeon_device *rdev,bool state);int r100_gpu_reset (struct radeon_device *rdev);int r100_mc_init (struct radeon_device *rdev);void r100_mc_fini (struct radeon_device *rdev);
@@ -81,6 +82,7 @@ static struct radeon_asic r100_asic = { .init = &r100_init, .errata = &r100_errata, .vram_info = &r100_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &r100_gpu_reset, .mc_init = &r100_mc_init, .mc_fini = &r100_mc_fini,
@@ -141,6 +143,7 @@ static struct radeon_asic r300_asic = { .init = &r300_init, .errata = &r300_errata, .vram_info = &r300_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &r300_gpu_reset, .mc_init = &r300_mc_init, .mc_fini = &r300_mc_fini,
@@ -181,6 +184,7 @@ static struct radeon_asic r420_asic = { .init = &r300_init, .errata = &r420_errata, .vram_info = &r420_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &r300_gpu_reset, .mc_init = &r420_mc_init, .mc_fini = &r420_mc_fini,
@@ -228,6 +232,7 @@ static struct radeon_asic rs400_asic = { .init = &r300_init, .errata = &rs400_errata, .vram_info = &rs400_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &r300_gpu_reset, .mc_init = &rs400_mc_init, .mc_fini = &rs400_mc_fini,
@@ -277,6 +282,7 @@ static struct radeon_asic rs600_asic = { .init = &r300_init, .errata = &rs600_errata, .vram_info = &rs600_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &r300_gpu_reset, .mc_init = &rs600_mc_init, .mc_fini = &rs600_mc_fini,
@@ -319,6 +325,7 @@ static struct radeon_asic rs690_asic = { .init = &r300_init, .errata = &rs690_errata, .vram_info = &rs690_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &r300_gpu_reset, .mc_init = &rs690_mc_init, .mc_fini = &rs690_mc_fini,
@@ -368,6 +375,7 @@ static struct radeon_asic rv515_asic = { .init = &rv515_init, .errata = &rv515_errata, .vram_info = &rv515_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &rv515_gpu_reset, .mc_init = &rv515_mc_init, .mc_fini = &rv515_mc_fini,
@@ -410,6 +418,7 @@ static struct radeon_asic r520_asic = { .init = &rv515_init, .errata = &r520_errata, .vram_info = &r520_vram_info, + .vga_set_state = &r100_vga_set_state, .gpu_reset = &rv515_gpu_reset, .mc_init = &r520_mc_init, .mc_fini = &r520_mc_fini,
drivers/gpu/drm/radeon/radeon_device.c +18 -0
@@ -29,6 +29,7 @@#include <drm/drmP.h> #include <drm/drm_crtc_helper.h> #include <drm/radeon_drm.h> +#include <linux/vgaarb.h> #include "radeon_reg.h" #include "radeon.h" #include "radeon_asic.h"
@@ -446,7 +447,18 @@ void radeon_combios_fini(struct radeon_device *rdev)int radeon_modeset_init (struct radeon_device *rdev);void radeon_modeset_fini (struct radeon_device *rdev); +/* if we get transitioned to only one device, tak VGA back */ +static unsigned int radeon_vga_set_decode (void *cookie,bool state) +{ +struct radeon_device *rdev = cookie; +radeon_vga_set_state (rdev, state); +if (state) +return VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM | +VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; +else +return VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; +}/* * Radeon device. */
@@ -535,6 +547,11 @@ int radeon_device_init(struct radeon_device *rdev,/* Initialize surface registers */ radeon_surface_init (rdev); +/* if we have > 1 VGA cards, then disable the radeon VGA resources */ + ret =vga_client_register (rdev->pdev, rdev,NULL , radeon_vga_set_decode); +if (ret) +return -EINVAL ; +/* TODO: disable VGA need to use VGA request */ /* BIOS*/ if (!radeon_get_bios (rdev)) {
@@ -669,6 +686,7 @@ void radeon_device_fini(struct radeon_device *rdev)radeon_agp_fini (rdev);#endif radeon_irq_kms_fini (rdev); +vga_client_register (rdev->pdev,NULL ,NULL ,NULL );radeon_fence_driver_fini (rdev);radeon_clocks_fini (rdev);if (rdev->is_atom_bios) {
include/drm/drmP.h +3 -0
@@ -786,6 +786,9 @@ struct drm_driver {int (*gem_init_object) (struct drm_gem_object *obj);void (*gem_free_object) (struct drm_gem_object *obj); +/* vga arb irq handler */ +void (*vgaarb_irq)(struct drm_device *dev,bool state); +/* Driver private ops for this object */ struct vm_operations_struct *gem_vm_ops;
Signature · 1 line
1.6.0.6
Dave Airlie
maintainerPATCH 1/1 vga: implements VGA arbitration on Linux
From: Tiago Vignatti <[email protected]>
Background:
Graphic devices are accessed through ranges in I/O or memory space. While most modern devices allow relocation of such ranges, some "Legacy" VGA devices implemented on PCI will typically have the same "hard-decoded" addresses as they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware Revision 2.1" Section 7, Legacy Devices.
The Resource Access Control (RAC) module inside the X server currently does the task of arbitration when more than one legacy device co-exists on the same machine. But the problem happens when these devices are trying to be accessed by different userspace clients (e.g. two server in parallel). Their address assignments conflict. Therefore an arbitration scheme _outside_ of the X server is needed to control the sharing of these resources. This document introduces the operation of the VGA arbiter implemented for Linux kernel.
--- drivers/gpu/Makefile | 2 +- drivers/gpu/vga/Kconfig | 10 + drivers/gpu/vga/Makefile | 1 + drivers/gpu/vga/vgaarb.c | 1206 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 44 ++ drivers/video/Kconfig | 2 + include/linux/pci.h | 2 + include/linux/vgaarb.h | 195 ++++++++ 8 files changed, 1461 insertions(+), 1 deletions(-) create mode 100644 drivers/gpu/vga/Kconfig create mode 100644 drivers/gpu/vga/Makefile create mode 100644 drivers/gpu/vga/vgaarb.c create mode 100644 include/linux/vgaarb.h
drivers/gpu/Makefile +1 -1
@@ -1 +1 @@ -obj-y += drm/ +obj-y += drm/ vga/
drivers/gpu/vga/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ +config VGA_ARB + bool "VGA Arbitration" if EMBEDDED + default y + depends on PCI + help + Some "legacy" VGA devices implemented on PCI typically have the same + hard-decoded addresses as they did on ISA. When multiple PCI devices + are accessed at same time they need some kind of coordination. Please + see Documentation/vgaarbiter.txt for more details. Select this to + enable VGA arbiter.
drivers/gpu/vga/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_VGA_ARB) += vgaarb.o
drivers/gpu/vga/vgaarb.c new file +1206 -0
1,206 lines not on this page · raw message
drivers/pci/pci.c +44 -0
50 lines not on this page · raw message
drivers/video/Kconfig +2 -0
@@ -7,6 +7,8 @@ menu "Graphics support" source "drivers/char/agp/Kconfig" +source "drivers/gpu/vga/Kconfig" + source "drivers/gpu/drm/Kconfig" config VGASTATE
include/linux/pci.h +2 -0
@@ -805,6 +805,8 @@ int pci_cfg_space_size_ext(struct pci_dev *dev);int pci_cfg_space_size (struct pci_dev *dev);unsigned char pci_bus_max_busnr (struct pci_bus *bus); +int pci_set_vga_state (struct pci_dev *pdev,bool decode, +unsigned int command_bits,bool change_bridge);/* kmem_cache style wrapper around pci_alloc_consistent() */ #include <linux/dmapool.h>
include/linux/vgaarb.h new file +195 -0
195 lines not on this page · raw message
Signature · 1 line
1.6.0.6
Hi,
some minor comments below.
Quoted 48 lines · On Tue, 11 Aug 2009 15:52:06 +1000 Dave Airlie <[email protected]> wrote:
From: Tiago Vignatti <[email protected]>
Background:
Graphic devices are accessed through ranges in I/O or memory space. While most modern devices allow relocation of such ranges, some "Legacy" VGA devices implemented on PCI will typically have the same "hard-decoded" addresses as they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware Revision 2.1" Section 7, Legacy Devices.The Resource Access Control (RAC) module inside the X server currently does the task of arbitration when more than one legacy device co-exists on the same machine. But the problem happens when these devices are trying to be accessed by different userspace clients (e.g. two server in parallel). Their address assignments conflict. Therefore an arbitration scheme _outside_ of the X server is needed to control the sharing of these resources. This document introduces the operation of the VGA arbiter implemented for Linux kernel.
Signed-off-by Tiago Vignatti <[email protected]>Signed-off-by Dave Airlie <[email protected]>--- drivers/gpu/Makefile | 2 +- drivers/gpu/vga/Kconfig | 10 + drivers/gpu/vga/Makefile | 1 + drivers/gpu/vga/vgaarb.c | 1206 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 44 ++ drivers/video/Kconfig | 2 + include/linux/pci.h | 2 + include/linux/vgaarb.h | 195 ++++++++ 8 files changed, 1461 insertions(+), 1 deletions(-) create mode 100644 drivers/gpu/vga/Kconfig create mode 100644 drivers/gpu/vga/Makefile create mode 100644 drivers/gpu/vga/vgaarb.c create mode 100644 include/linux/vgaarb.hdrivers/gpu/Makefile +1
-1@@ -1 +1 @@ -obj-y += drm/ +obj-y += drm/ vga/drivers/gpu/vga/Kconfig new file +0
-0
drivers/gpu/vga/Kconfig line 10
Quoted 10 lines
drivers/gpu/vga/Kconfig new file +10
-0@@ -0,0 +1,10 @@ +config VGA_ARB + bool "VGA Arbitration" if EMBEDDED + default y + depends on PCI + help + Some "legacy" VGA devices implemented on PCI typically have the same + hard-decoded addresses as they did on ISA. When multiple PCI devices + are accessed at same time they need some kind of coordination. Please + see Documentation/vgaarbiter.txt for more details. Select this to + enable VGA arbiter.
The file Documentation/vgaarbiter.txt does not exist.
Quoted 13 lines
drivers/gpu/vga/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-$(CONFIG_VGA_ARB) += vgaarb.odrivers/gpu/vga/vgaarb.c new file +0
-0
drivers/gpu/vga/vgaarb.c line 9
Quoted 9 lines
drivers/gpu/vga/vgaarb.c new file +9
-0@@ -0,0 +1,1206 @@ +/* +* vgaarb.c +* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]> +* (C) Copyright 2007 Paulo R. Zanoni <[email protected]> +* (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]> +* +* Implements the VGA arbitration. For details refer to +* Documentation/vgaarbiter.txt
Another reference to vgaarbiter.txt.
Quoted 150 lines
drivers/gpu/vga/vgaarb.c new file +150
-0+*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/pci.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/wait.h> +#include <linux/spinlock.h> +#include <linux/poll.h> +#include <linux/miscdevice.h> + +#include <linux/uaccess.h> + +#include <linux/vgaarb.h> + +static void vga_arbiter_notify_clients (void ); +/* +* We keep a list of all vga devices in the system to speed +* up the various operations of the arbiter +*/ +struct vga_device { +struct list_head list; +struct pci_dev *pdev; +unsigned int decodes;/* what does it decodes */ +unsigned int owns;/* what does it owns */ +unsigned int locks;/* what does it locks */ +unsigned int io_lock_cnt;/* legacy IO lock count */ +unsigned int mem_lock_cnt;/* legacy MEM lock count */ +unsigned int io_norm_cnt;/* normal IO count */ +unsigned int mem_norm_cnt;/* normal MEM count */ + +/* allow IRQ enable/disable hook */ +void *cookie; +void (*irq_set_state)(void *cookie,bool enable); +unsigned int (*set_vga_decode)(void *cookie,bool decode); +}; + +static LIST_HEAD (vga_list); +static int vga_count, vga_decode_count; +static bool vga_arbiter_used; +static DEFINE_SPINLOCK (vga_lock); +static DECLARE_WAIT_QUEUE_HEAD (vga_wait_queue); + + +static const char *vga_iostate_to_str (unsigned int iostate) +{ +/* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ + iostate &=VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +switch (iostate) { +case VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM : +return "io+mem" ; +case VGA_RSRC_LEGACY_IO : +return "io" ; +case VGA_RSRC_LEGACY_MEM : +return "mem" ; + } +return "none" ; +} + +static int vga_str_to_iostate (char *buf,int str_size,int *io_state) +{ +/* we could in theory hand out locks on IO and mem +* separately to userspace but it can cause deadlocks */ +if (strncmp (buf,"none" ,4 ) ==0 ) { + *io_state =VGA_RSRC_NONE ; +return 1 ; + } + +/* XXX We're not chekcing the str_size! */ +if (strncmp (buf,"io+mem" ,6 ) ==0 ) + goto both; +else if (strncmp (buf,"io" ,2 ) ==0 ) + goto both; +else if (strncmp (buf,"mem" ,3 ) ==0 ) + goto both; +return 0 ; +both: + *io_state =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +return 1 ; +} + +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +/* this is only used a cookie - it should not be dereferenced */ +static struct pci_dev *vga_default; +#endif + +static void vga_arb_device_card_gone (struct pci_dev *pdev); + +/* Find somebody in our list */ +static struct vga_device *vgadev_find (struct pci_dev *pdev) +{ +struct vga_device *vgadev; + +list_for_each_entry (vgadev, &vga_list, list) +if (pdev == vgadev->pdev) +return vgadev; +return NULL ; +} + +/* Returns the default VGA device (vgacon's babe) */ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +struct pci_dev *vga_default_device (void ) +{ +return vga_default; +} +#endif + +static inline void vga_irq_set_state (struct vga_device *vgadev,bool state) +{ +if (vgadev->irq_set_state) + vgadev->irq_set_state (vgadev->cookie, state); +} + + +/* If we don't ever use VGA arb we should avoid +turning off anything anywhere due to old X servers getting +confused about the boot device not being VGA */ +static void vga_check_first_use (void ) +{ +/* we should inform all GPUs in the system that +* VGA arb has occured and to try and disable resources +* if they can */ +if (!vga_arbiter_used) { + vga_arbiter_used = true; +vga_arbiter_notify_clients (); + } +} + +static struct vga_device *__vga_tryget (struct vga_device *vgadev, +unsigned int rsrc) +{ +unsigned int wants, legacy_wants, match; +struct vga_device *conflict; +unsigned int pci_bits; +/* Account for "normal" resources to lock. If we decode the legacy, +* counterpart, we need to request it as well +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && + (vgadev->decodes &VGA_RSRC_LEGACY_IO )) + rsrc |=VGA_RSRC_LEGACY_IO ; +if ((rsrc &VGA_RSRC_NORMAL_MEM ) && + (vgadev->decodes &VGA_RSRC_LEGACY_MEM )) + rsrc |=VGA_RSRC_LEGACY_MEM ; + +pr_devel ("%s: %d\n" , __func__, rsrc); +pr_devel ("%s: owns: %d\n" , __func__, vgadev->owns); +
drivers/gpu/vga/vgaarb.c line 169
Quoted 10 lines
drivers/gpu/vga/vgaarb.c new file +10
-0+/* Check what resources we need to acquire */ + wants = rsrc & ~vgadev->owns; + +/* We already own everything, just mark locked & bye bye */ +if (wants ==0 ) + goto lock_them; + +/* We don't need to request a legacy resource, we just enable +* appropriate decoding and go +*/
Does this comment mean "We don't need to request a legacy resource, therefore ..." or "If a legacy resource is not required, ..."? Well, it gets clear when I read and think about the code, but the comment got me confused for a moment.
drivers/gpu/vga/vgaarb.c line 174
Quoted 5 lines
drivers/gpu/vga/vgaarb.c new file +5
-0+ legacy_wants = wants &VGA_RSRC_LEGACY_MASK ; +if (legacy_wants ==0 ) + goto enable_them; + +/* Ok, we don't, let's find out how we need to kick off */
We don't what?
Quoted 428 lines
drivers/gpu/vga/vgaarb.c new file +428
-0+list_for_each_entry (conflict, &vga_list, list) { +unsigned int lwants = legacy_wants; +unsigned int change_bridge =0 ; + +/* Don't conflict with myself */ +if (vgadev == conflict) +continue ; + +/* Check if the architecture allows a conflict between those +* 2 devices or if they are on separate domains +*/ +if (!vga_conflicts (vgadev->pdev, conflict->pdev)) +continue ; + +/* We have a possible conflict. before we go further, we must +* check if we sit on the same bus as the conflicting device. +* if we don't, then we must tie both IO and MEM resources +* together since there is only a single bit controlling +* VGA forwarding on P2P bridges +*/ +if (vgadev->pdev->bus != conflict->pdev->bus) { + change_bridge =1 ; + lwants =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; + } + +/* Check if the guy has a lock on the resource. If he does, +* return the conflicting entry +*/ +if (conflict->locks & lwants) +return conflict; + +/* Ok, now check if he owns the resource we want. We don't need +* to check "decodes" since it should be impossible to own +* own legacy resources you don't decode unless I have a bug +* in this code... +*/ +WARN_ON (conflict->owns & ~conflict->decodes); + match = lwants & conflict->owns; +if (!match) +continue ; + +/* looks like he doesn't have a lock, we can steal +* them from him +*/ +vga_irq_set_state (conflict, false); + + pci_bits =0 ; +if (lwants & (VGA_RSRC_LEGACY_MEM |VGA_RSRC_NORMAL_MEM )) + pci_bits |=PCI_COMMAND_MEMORY ; +if (lwants & (VGA_RSRC_LEGACY_IO |VGA_RSRC_NORMAL_IO )) + pci_bits |=PCI_COMMAND_IO ; + +pci_set_vga_state (conflict->pdev, false, pci_bits, + change_bridge); + conflict->owns &= ~lwants; +/* If he also owned non-legacy, that is no longer the case */ +if (lwants &VGA_RSRC_LEGACY_MEM ) + conflict->owns &= ~VGA_RSRC_NORMAL_MEM ; +if (lwants &VGA_RSRC_LEGACY_IO ) + conflict->owns &= ~VGA_RSRC_NORMAL_IO ; + } + +enable_them: +/* ok dude, we got it, everybody conflicting has been disabled, let's +* enable us. Make sure we don't mark a bit in "owns" that we don't +* also have in "decodes". We can lock resources we don't decode but +* not own them. +*/ + pci_bits =0 ; +if (wants & (VGA_RSRC_LEGACY_MEM |VGA_RSRC_NORMAL_MEM )) + pci_bits |=PCI_COMMAND_MEMORY ; +if (wants & (VGA_RSRC_LEGACY_IO |VGA_RSRC_NORMAL_IO )) + pci_bits |=PCI_COMMAND_IO ; +pci_set_vga_state (vgadev->pdev, true, pci_bits, !!(wants &VGA_RSRC_LEGACY_MASK )); + +vga_irq_set_state (vgadev, true); + vgadev->owns |= (wants & vgadev->decodes); +lock_them: + vgadev->locks |= (rsrc &VGA_RSRC_LEGACY_MASK ); +if (rsrc &VGA_RSRC_LEGACY_IO ) + vgadev->io_lock_cnt++; +if (rsrc &VGA_RSRC_LEGACY_MEM ) + vgadev->mem_lock_cnt++; +if (rsrc &VGA_RSRC_NORMAL_IO ) + vgadev->io_norm_cnt++; +if (rsrc &VGA_RSRC_NORMAL_MEM ) + vgadev->mem_norm_cnt++; + +return NULL ; +} + +static void __vga_put (struct vga_device *vgadev,unsigned int rsrc) +{ +unsigned int old_locks = vgadev->locks; + +pr_devel ("%s\n" , __func__); + +/* Update our counters, and account for equivalent legacy resources +* if we decode them +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && vgadev->io_norm_cnt >0 ) { + vgadev->io_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_IO ) + rsrc |=VGA_RSRC_LEGACY_IO ; + } +if ((rsrc &VGA_RSRC_NORMAL_MEM ) && vgadev->mem_norm_cnt >0 ) { + vgadev->mem_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_MEM ) + rsrc |=VGA_RSRC_LEGACY_MEM ; + } +if ((rsrc &VGA_RSRC_LEGACY_IO ) && vgadev->io_lock_cnt >0 ) + vgadev->io_lock_cnt--; +if ((rsrc &VGA_RSRC_LEGACY_MEM ) && vgadev->mem_lock_cnt >0 ) + vgadev->mem_lock_cnt--; + +/* Just clear lock bits, we do lazy operations so we don't really +* have to bother about anything else at this point +*/ +if (vgadev->io_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_IO ; +if (vgadev->mem_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_MEM ; + +/* Kick the wait queue in case somebody was waiting if we actually +* released something +*/ +if (old_locks != vgadev->locks) +wake_up_all (&vga_wait_queue); +} + +int vga_get (struct pci_dev *pdev,unsigned int rsrc,int interruptible) +{ +struct vga_device *vgadev, *conflict; +unsigned long flags; +wait_queue_t wait; +int rc =0 ; + +vga_check_first_use (); +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; + +for (;;) { +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +spin_unlock_irqrestore (&vga_lock, flags); + rc = -ENODEV ; +break ; + } + conflict =__vga_tryget (vgadev, rsrc); +spin_unlock_irqrestore (&vga_lock, flags); +if (conflict ==NULL ) +break ; + + +/* We have a conflict, we wait until somebody kicks the +* work queue. Currently we have one work queue that we +* kick each time some resources are released, but it would +* be fairly easy to have a per device one so that we only +* need to attach to the conflicting device +*/ +init_waitqueue_entry (&wait, current); +add_wait_queue (&vga_wait_queue, &wait); +set_current_state (interruptible ? +TASK_INTERRUPTIBLE : +TASK_UNINTERRUPTIBLE ); +if (signal_pending (current)) { + rc = -EINTR ; +break ; + } +schedule (); +remove_wait_queue (&vga_wait_queue, &wait); +set_current_state (TASK_RUNNING ); + } +return rc; +} +EXPORT_SYMBOL (vga_get); + +int vga_tryget (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; +int rc =0 ; + +vga_check_first_use (); + +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + rc = -ENODEV ; + goto bail; + } +if (__vga_tryget (vgadev, rsrc)) + rc = -EBUSY ; +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return rc; +} +EXPORT_SYMBOL (vga_tryget); + +void vga_put (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; + +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; +__vga_put (vgadev, rsrc); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +} +EXPORT_SYMBOL (vga_put); + +/* +* Currently, we assume that the "initial" setup of the system is +* not sane, that is we come up with conflicting devices and let +* the arbiter's client decides if devices decodes or not legacy +* things. +*/ +static bool vga_arbiter_add_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + +/* Only deal with VGA class devices */ +if ((pdev->class >>8 ) !=PCI_CLASS_DISPLAY_VGA ) +return false; + +/* Allocate structure */ + vgadev =kmalloc (sizeof (struct vga_device ),GFP_KERNEL ); +if (vgadev ==NULL ) { +pr_err ("vgaarb: failed to allocate pci device\n" ); +/* What to do on allocation failure ? For now, let's +* just do nothing, I'm not sure there is anything saner +* to be done +*/ +return false; + } + +memset (vgadev,0 ,sizeof (*vgadev)); + +/* Take lock & check for duplicates */ +spin_lock_irqsave (&vga_lock, flags); +if (vgadev_find (pdev) !=NULL ) { +BUG_ON (1 ); + goto fail; + } + vgadev->pdev = pdev; + +/* By default, assume we decode everything */ + vgadev->decodes =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM | +VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; + +/* by default mark it as decoding */ + vga_decode_count++; +/* Mark that we "own" resources based on our enables, we will +* clear that below if the bridge isn't forwarding +*/ +pci_read_config_word (pdev,PCI_COMMAND , &cmd); +if (cmd &PCI_COMMAND_IO ) + vgadev->owns |=VGA_RSRC_LEGACY_IO ; +if (cmd &PCI_COMMAND_MEMORY ) + vgadev->owns |=VGA_RSRC_LEGACY_MEM ; + +/* Check if VGA cycles can get down to us */ + bus = pdev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { +u16 l; +pci_read_config_word (bridge,PCI_BRIDGE_CONTROL , + &l); +if (!(l &PCI_BRIDGE_CTL_VGA )) { + vgadev->owns =0 ; +break ; + } + } + bus = bus->parent; + } + +/* Deal with VGA default device. Use first enabled one +* by default if arch doesn't have it's own hook +*/ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +if (vga_default ==NULL && + ((vgadev->owns &VGA_RSRC_LEGACY_MASK ) ==VGA_RSRC_LEGACY_MASK )) + vga_default =pci_dev_get (pdev); +#endif + +/* Add to the list */ +list_add (&vgadev->list, &vga_list); + vga_count++; +pr_info ("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n" , +pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), +vga_iostate_to_str (vgadev->locks)); + +spin_unlock_irqrestore (&vga_lock, flags); +return true; +fail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return false; +} + +static bool vga_arbiter_del_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +bool ret = true; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + ret = false; + goto bail; + } + +if (vga_default == pdev) { +pci_dev_put (vga_default); + vga_default =NULL ; + } + +if (vgadev->decodes & (VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM )) + vga_decode_count--; + +/* Remove entry from list */ +list_del (&vgadev->list); + vga_count--; +/* Notify userland driver that the device is gone so it discards +* it's copies of the pci_dev pointer +*/ +vga_arb_device_card_gone (pdev); + +/* Wake up all possible waiters */ +wake_up_all (&vga_wait_queue); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return ret; +} + +/* this is called with the lock */ +static inline void vga_update_device_decodes (struct vga_device *vgadev, +int new_decodes) +{ +int old_decodes; +struct vga_device *new_vgadev, *conflict; + + old_decodes = vgadev->decodes; + vgadev->decodes = new_decodes; + +pr_info ("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n" , +pci_name (vgadev->pdev), +vga_iostate_to_str (old_decodes), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns)); + + +/* if we own the decodes we should move them along to +another card */ +if ((vgadev->owns & old_decodes) && (vga_count >1 )) { +/* set us to own nothing */ + vgadev->owns &= ~old_decodes; +list_for_each_entry (new_vgadev, &vga_list, list) { +if ((new_vgadev != vgadev) && + (new_vgadev->decodes &VGA_RSRC_LEGACY_MASK )) { +pr_info ("vgaarb: transferring owner from PCI:%s to PCI:%s\n" ,pci_name (vgadev->pdev),pci_name (new_vgadev->pdev)); + conflict =__vga_tryget (new_vgadev,VGA_RSRC_LEGACY_MASK ); +if (!conflict) +__vga_put (new_vgadev,VGA_RSRC_LEGACY_MASK ); +break ; + } + } + } + +/* change decodes counter */ +if (old_decodes != new_decodes) { +if (new_decodes & (VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM )) + vga_decode_count++; +else + vga_decode_count--; + } +} + +void __vga_set_legacy_decoding (struct pci_dev *pdev,unsigned int decodes,bool userspace) +{ +struct vga_device *vgadev; +unsigned long flags; + + decodes &=VGA_RSRC_LEGACY_MASK ; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; + +/* don't let userspace futz with kernel driver decodes */ +if (userspace && vgadev->set_vga_decode) + goto bail; + +/* update the device decodes + counter */ +vga_update_device_decodes (vgadev, decodes); + +/* XXX if somebody is going from "doesn't decode" to "decodes" state +* here, additional care must be taken as we may have pending owner +* ship of non-legacy region ... +*/ +bail:
drivers/gpu/vga/vgaarb.c line 612
Quoted 10 lines
drivers/gpu/vga/vgaarb.c new file +10
-0+spin_unlock_irqrestore (&vga_lock, flags); +} + +void vga_set_legacy_decoding (struct pci_dev *pdev,unsigned int decodes) +{ +__vga_set_legacy_decoding (pdev, decodes, false); +} +EXPORT_SYMBOL (vga_set_legacy_decoding); + +/* return number of active VGA devices */
That comment is incorrect.
Quoted 418 lines
drivers/gpu/vga/vgaarb.c new file +418
-0+/* call with NULL to unregister */ +int vga_client_register (struct pci_dev *pdev,void *cookie, +void (*irq_set_state)(void *cookie,bool state), +unsigned int (*set_vga_decode)(void *cookie,bool decode)) +{ +int ret =-1 ; +struct vga_device *vgadev; +unsigned long flags; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (!vgadev) + goto bail; + + vgadev->irq_set_state = irq_set_state; + vgadev->set_vga_decode = set_vga_decode; + vgadev->cookie = cookie; + ret =0 ; + +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return ret; + +} +EXPORT_SYMBOL (vga_client_register); + +/* +* Char driver implementation +* +* Semantics is: +* +* open : open user instance of the arbitrer. by default, it's +* attached to the default VGA device of the system. +* +* close : close user instance, release locks +* +* read : return a string indicating the status of the target. +* an IO state string is of the form {io,mem,io+mem,none}, +* mc and ic are respectively mem and io lock counts (for +* debugging/diagnostic only). "decodes" indicate what the +* card currently decodes, "owns" indicates what is currently +* enabled on it, and "locks" indicates what is locked by this +* card. If the card is unplugged, we get "invalid" then for +* card_ID and an -ENODEV error is returned for any command +* until a new card is targeted +* +* "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)" +* +* write : write a command to the arbiter. List of commands is: +* +* target <card_ID> : switch target to card <card_ID> (see below) +* lock <io_state> : acquires locks on target ("none" is invalid io_state) +* trylock <io_state> : non-blocking acquire locks on target +* unlock <io_state> : release locks on target +* unlock all : release all locks on target held by this user +* decodes <io_state> : set the legacy decoding attributes for the card +* +* poll : event if something change on any card (not just the target) +* +* card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default" +* to go back to the system default card (TODO: not implemented yet). +* Currently, only PCI is supported as a prefix, but the userland API may +* support other bus types in the future, even if the current kernel +* implementation doesn't. +* +* Note about locks: +* +* The driver keeps track of which user has what locks on which card. It +* supports stacking, like the kernel one. This complexifies the implementation +* a bit, but makes the arbiter more tolerant to userspace problems and able +* to properly cleanup in all cases when a process dies. +* Currently, a max of 16 cards simultaneously can have locks issued from +* userspace for a given user (file descriptor instance) of the arbiter. +* +* If the device is hot-unplugged, there is a hook inside the module to notify +* they being added/removed in the system and automatically added/removed in +* the arbiter. +*/ + +#define MAX_USER_CARDS 16 +#define PCI_INVALID_CARD ((struct pci_dev *)-1UL) + +/* +* Each user has an array of these, tracking which cards have locks +*/ +struct vga_arb_user_card { +struct pci_dev *pdev; +unsigned int mem_cnt; +unsigned int io_cnt; +}; + +struct vga_arb_private { +struct list_head list; +struct pci_dev *target; +struct vga_arb_user_card cards[MAX_USER_CARDS ]; +spinlock_t lock; +}; + +static LIST_HEAD (vga_user_list); +static DEFINE_SPINLOCK (vga_user_lock); + + +/* +* This function gets a string in the format: "PCI:domain:bus:dev.fn" and +* returns the respective values. If the string is not in this format, +* it returns 0. +*/ +static int vga_pci_str_to_vars (char *buf,int count,unsigned int *domain, +unsigned int *bus,unsigned int *devfn) +{ +int n; +unsigned int slot, func; + + + n =sscanf (buf,"PCI:%x:%x:%x.%x" , domain, bus, &slot, &func); +if (n !=4 ) +return 0 ; + + *devfn =PCI_DEVFN (slot, func); + +return 1 ; +} + +static ssize_t vga_arb_read (struct file *file,char __user * buf, +size_t count,loff_t *ppos) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_device *vgadev; +struct pci_dev *pdev; +unsigned long flags; +size_t len; +int rc; +char *lbuf; + + lbuf =kmalloc (1024 ,GFP_KERNEL ); +if (lbuf ==NULL ) +return -ENOMEM ; + +/* Shields against vga_arb_device_card_gone (pci_dev going +* away), and allows access to vga list +*/ +spin_lock_irqsave (&vga_lock, flags); + +/* If we are targetting the default, use it */ + pdev = priv->target; +if (pdev ==NULL || pdev ==PCI_INVALID_CARD ) { +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Find card vgadev structure */ + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +/* Wow, it's not in the list, that shouldn't happen, +* let's fix us up and return invalid card +*/ +if (pdev == priv->target) +vga_arb_device_card_gone (pdev); +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Fill the buffer with infos */ + len =snprintf (lbuf,1024 , +"count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n" , + vga_decode_count,pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), +vga_iostate_to_str (vgadev->locks), + vgadev->io_lock_cnt, vgadev->mem_lock_cnt); + +spin_unlock_irqrestore (&vga_lock, flags); +done: + +/* Copy that to user */ +if (len > count) + len = count; + rc =copy_to_user (buf, lbuf, len); +kfree (lbuf); +if (rc) +return -EFAULT ; +return len; +} + +/* +* TODO: To avoid parsing inside kernel and to improve the speed we may +* consider use ioctl here +*/ +static ssize_t vga_arb_write (struct file *file,const char __user * buf, +size_t count,loff_t *ppos) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc =NULL ; +struct pci_dev *pdev; + +unsigned int io_state; + +char *kbuf, *curr_pos; +size_t remaining = count; + +int ret_val; +int i; + + + kbuf =kmalloc (count +1 ,GFP_KERNEL ); +if (!kbuf) +return -ENOMEM ; + +if (copy_from_user (kbuf, buf, count)) { +kfree (kbuf); +return -EFAULT ; + } + curr_pos = kbuf; + kbuf[count] ='\0' ;/* Just to make sure... */ + +if (strncmp (curr_pos,"lock " ,5 ) ==0 ) { + curr_pos +=5 ; + remaining -=5 ; + +pr_devel ("client 0x%X called 'lock'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +if (io_state ==VGA_RSRC_NONE ) { + ret_val = -EPROTO ; + goto done; + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +vga_get_uninterruptible (pdev, io_state); + +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { +if (io_state &VGA_RSRC_LEGACY_IO ) + priv->cards[i].io_cnt++; +if (io_state &VGA_RSRC_LEGACY_MEM ) + priv->cards[i].mem_cnt++; +break ; + } + } + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"unlock " ,7 ) ==0 ) { + curr_pos +=7 ; + remaining -=7 ; + +pr_devel ("client 0x%X called 'unlock'\n" , (int )priv); + +if (strncmp (curr_pos,"all" ,3 ) ==0 ) + io_state =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +else { +if (!vga_str_to_iostate + (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) + uc = &priv->cards[i]; + } + +if (!uc) +return -EINVAL ; + +if (io_state &VGA_RSRC_LEGACY_IO && uc->io_cnt ==0 ) +return -EINVAL ; + +if (io_state &VGA_RSRC_LEGACY_MEM && uc->mem_cnt ==0 ) +return -EINVAL ; + +vga_put (pdev, io_state); + +if (io_state &VGA_RSRC_LEGACY_IO ) + uc->io_cnt--; +if (io_state &VGA_RSRC_LEGACY_MEM ) + uc->mem_cnt--; + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"trylock " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; + +pr_devel ("client 0x%X called 'trylock'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +if (vga_tryget (pdev, io_state)) { +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { +if (io_state &VGA_RSRC_LEGACY_IO ) + priv->cards[i].io_cnt++; +if (io_state &VGA_RSRC_LEGACY_MEM ) + priv->cards[i].mem_cnt++; +break ; + } + } + ret_val = count; + goto done; + }else { + ret_val = -EBUSY ; + goto done; + } + + }else if (strncmp (curr_pos,"target " ,7 ) ==0 ) { +unsigned int domain, bus, devfn; +struct vga_device *vgadev; + + curr_pos +=7 ; + remaining -=7 ; +pr_devel ("client 0x%X called 'target'\n" , (int )priv); +/* if target is default */ +if (!strncmp (buf,"default" ,7 )) + pdev =pci_dev_get (vga_default_device ()); +else { +if (!vga_pci_str_to_vars (curr_pos, remaining, + &domain, &bus, &devfn)) { + ret_val = -EPROTO ; + goto done; + } + + pdev =pci_get_bus_and_slot (bus, devfn); +if (!pdev) { +pr_info ("vgaarb: invalid PCI address!\n" ); + ret_val = -ENODEV ; + goto done; + } + } + + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +pr_info ("vgaarb: this pci device is not a vga device\n" ); +pci_dev_put (pdev); + ret_val = -ENODEV ; + goto done; + } + + priv->target = pdev; +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) +break ; +if (priv->cards[i].pdev ==NULL ) { + priv->cards[i].pdev = pdev; + priv->cards[i].io_cnt =0 ; + priv->cards[i].mem_cnt =0 ; +break ; + } + } +if (i ==MAX_USER_CARDS ) { +pr_err ("vgaarb: maximum user cards number reached!\n" ); +pci_dev_put (pdev); +/* XXX: which value to return? */ + ret_val = -ENOMEM ; + goto done; + } + + ret_val = count; +pci_dev_put (pdev); + goto done; + + + }else if (strncmp (curr_pos,"decodes " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; +pr_devel ("vgaarb: client 0x%X called 'decodes'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +__vga_set_legacy_decoding (pdev, io_state, true); + ret_val = count;
drivers/gpu/vga/vgaarb.c line 1040
Quoted 10 lines
drivers/gpu/vga/vgaarb.c new file +10
-0+ goto done; + } +/* If we got here, the message written is not part of the protocol! */ +kfree (kbuf); +return -EPROTO ; + +done: +kfree (kbuf); +return ret_val; +}
How about splitting those else-if case implementations into separate functions?
Quoted 294 lines
drivers/gpu/vga/vgaarb.c new file +166
-0+ +static unsigned int vga_arb_fpoll (struct file *file,poll_table * wait) +{ +struct vga_arb_private *priv = file->private_data; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; +poll_wait (file, &vga_wait_queue, wait); +return POLLIN ; +} + +static int vga_arb_open (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv; +unsigned long flags; + +pr_devel ("%s\n" , __func__); + + priv =kmalloc (sizeof (struct vga_arb_private ),GFP_KERNEL ); +if (priv ==NULL ) +return -ENOMEM ; +memset (priv,0 ,sizeof (*priv)); +spin_lock_init (&priv->lock); + file->private_data = priv; + +spin_lock_irqsave (&vga_user_lock, flags); +list_add (&priv->list, &vga_user_list); +spin_unlock_irqrestore (&vga_user_lock, flags); + +/* Set the client' lists of locks */ + priv->target =vga_default_device ();/* Maybe this is still null! */ + priv->cards[0 ].pdev = priv->target; + priv->cards[0 ].io_cnt =0 ; + priv->cards[0 ].mem_cnt =0 ; + + +return 0 ; +} + +static int vga_arb_release (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc; +unsigned long flags; +int i; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; + +spin_lock_irqsave (&vga_user_lock, flags); +list_del (&priv->list); +for (i =0 ; i <MAX_USER_CARDS ; i++) { + uc = &priv->cards[i]; +if (uc->pdev ==NULL ) +continue ; +pr_devel ("uc->io_cnt == %d, uc->mem_cnt == %d\n" , + uc->io_cnt, uc->mem_cnt); +while (uc->io_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_IO ); +while (uc->mem_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_MEM ); + } +spin_unlock_irqrestore (&vga_user_lock, flags); + +kfree (priv); + +return 0 ; +} + +static void vga_arb_device_card_gone (struct pci_dev *pdev) +{ +} + +/* +* callback any registered clients to let them know we have a +* change in VGA cards +*/ +static void vga_arbiter_notify_clients (void ) +{ +struct vga_device *vgadev; +unsigned long flags; +uint32_t new_decodes; +bool new_state; + +if (!vga_arbiter_used) +return ; + +spin_lock_irqsave (&vga_lock, flags); +list_for_each_entry (vgadev, &vga_list, list) { +if (vga_count >1 ) + new_state = false; +else + new_state = true; +if (vgadev->set_vga_decode) { + new_decodes = vgadev->set_vga_decode (vgadev->cookie, new_state); +vga_update_device_decodes (vgadev, new_decodes); + } + } +spin_unlock_irqrestore (&vga_lock, flags); +} + +static int pci_notify (struct notifier_block *nb,unsigned long action, +void *data) +{ +struct device *dev = data; +struct pci_dev *pdev =to_pci_dev (dev); +bool notify = false; + +pr_devel ("%s\n" , __func__); + +/* For now we're only intereted in devices added and removed. I didn't +* test this thing here, so someone needs to double check for the +* cases of hotplugable vga cards. */ +if (action ==BUS_NOTIFY_ADD_DEVICE ) + notify =vga_arbiter_add_pci_device (pdev); +else if (action ==BUS_NOTIFY_DEL_DEVICE ) + notify =vga_arbiter_del_pci_device (pdev); + +if (notify) +vga_arbiter_notify_clients (); +return 0 ; +} + +static struct notifier_block pci_notifier = { + .notifier_call = pci_notify, +}; + +static const struct file_operations vga_arb_device_fops = { + .read = vga_arb_read, + .write = vga_arb_write, + .poll = vga_arb_fpoll, + .open = vga_arb_open, + .release = vga_arb_release, +}; + +static struct miscdevice vga_arb_device = { +MISC_DYNAMIC_MINOR ,"vga_arbiter" , &vga_arb_device_fops +}; + +static int __init vga_arb_device_init (void ) +{ +int rc; +struct pci_dev *pdev; + + rc =misc_register (&vga_arb_device); +if (rc <0 ) +pr_err ("vgaarb: error %d registering device\n" , rc); + +bus_register_notifier (&pci_bus_type, &pci_notifier); + +/* We add all pci devices satisfying vga class in the arbiter by +* default */ + pdev =NULL ; +while ((pdev = +pci_get_subsys (PCI_ANY_ID ,PCI_ANY_ID ,PCI_ANY_ID , +PCI_ANY_ID , pdev)) !=NULL ) +vga_arbiter_add_pci_device (pdev); + +pr_info ("vgaarb: loaded\n" ); +return rc; +} +subsys_initcall (vga_arb_device_init);drivers/pci/pci.c +44
-0@@ -2502,6 +2502,50 @@ int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)return 0 ; } +/** +* pci_set_vga_state - set VGA decode state on device and parents if requested +* @dev the PCI device +* @decode - true = enable decoding, false = disable decoding +* @command_bits PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY +* @change_bridge - traverse ancestors and change bridges +*/ +int pci_set_vga_state (struct pci_dev *dev,bool decode, +unsigned int command_bits,bool change_bridge) +{ +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + +WARN_ON (command_bits & ~(PCI_COMMAND_IO |PCI_COMMAND_MEMORY )); + +pci_read_config_word (dev,PCI_COMMAND , &cmd); +if (decode == true) + cmd |= command_bits; +else + cmd &= ~command_bits; +pci_write_config_word (dev,PCI_COMMAND , cmd); + +if (change_bridge == false) +return 0 ; + + bus = dev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { +pci_read_config_word (bridge,PCI_BRIDGE_CONTROL , + &cmd); +if (decode == true) + cmd |=PCI_BRIDGE_CTL_VGA ; +else + cmd &= ~PCI_BRIDGE_CTL_VGA ; +pci_write_config_word (bridge,PCI_BRIDGE_CONTROL , + cmd); + } + bus = bus->parent; + } +return 0 ; +} +#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZEstatic char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE ] = {0 };spinlock_t resource_alignment_lock =SPIN_LOCK_UNLOCKED ;drivers/video/Kconfig +2
-0@@ -7,6 +7,8 @@ menu "Graphics support" source "drivers/char/agp/Kconfig" +source "drivers/gpu/vga/Kconfig" + source "drivers/gpu/drm/Kconfig" config VGASTATEinclude/linux/pci.h +2
-0@@ -805,6 +805,8 @@ int pci_cfg_space_size_ext(struct pci_dev *dev);int pci_cfg_space_size (struct pci_dev *dev);unsigned char pci_bus_max_busnr (struct pci_bus *bus); +int pci_set_vga_state (struct pci_dev *pdev,bool decode, +unsigned int command_bits,bool change_bridge);/* kmem_cache style wrapper around pci_alloc_consistent() */ #include <linux/dmapool.h> include/linux/vgaarb.h new file +41
-0@@ -0,0 +1,195 @@ +/* +* vgaarb.c +* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]> +* (C) Copyright 2007 Paulo R. Zanoni <[email protected]> +* (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]> +*/ + +#ifndef LINUX_VGA_H + +#include <asm/vga.h> + +/* Legacy VGA regions */ +#define VGA_RSRC_NONE 0x00 +#define VGA_RSRC_LEGACY_IO 0x01 +#define VGA_RSRC_LEGACY_MEM 0x02 +#define VGA_RSRC_LEGACY_MASK (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM) +/* Non-legacy access */ +#define VGA_RSRC_NORMAL_IO 0x04 +#define VGA_RSRC_NORMAL_MEM 0x08 + +/* Passing that instead of a pci_dev to use the system "default" +* device, that is the one used by vgacon. Archs will probably +* have to provide their own vga_default_device(); +*/ +#define VGA_DEFAULT_DEVICE (NULL) + +/* For use by clients */ + +/** +* vga_set_legacy_decoding +* +* @pdev: pci device of the VGA card +* @decodes: bit mask of what legacy regions the card decodes +* +* Indicates to the arbiter if the card decodes legacy VGA IOs, +* legacy VGA Memory, both, or none. All cards default to both, +* the card driver (fbdev for example) should tell the arbiter +* if it has disabled legacy decoding, so the card can be left +* out of the arbitration process (and can be safe to take +* interrupts at any time.
include/linux/vgaarb.h line 51
Quoted 10 lines
include/linux/vgaarb.h new file +10
-0+*/ +extern void vga_set_legacy_decoding (struct pci_dev *pdev, +unsigned int decodes); + +/** +* vga_get - acquire & locks VGA resources +* +* pdev: pci device of the VGA card or NULL for the system default +* rsrc: bit mask of resources to acquire and lock +* interruptible: blocking should be interruptible by signals ?
I don't know the kerneldoc format, but looks like argument names should be prefixed with @.
Quoted 29 lines
include/linux/vgaarb.h new file +29
-0+* +* This function acquires VGA resources for the given +* card and mark those resources locked. If the resource requested +* are "normal" (and not legacy) resources, the arbiter will first check +* wether the card is doing legacy decoding for that type of resource. If +* yes, the lock is "converted" into a legacy resource lock. +* The arbiter will first look for all VGA cards that might conflict +* and disable their IOs and/or Memory access, inlcuding VGA forwarding +* on P2P bridges if necessary, so that the requested resources can +* be used. Then, the card is marked as locking these resources and +* the IO and/or Memory accesse are enabled on the card (including +* VGA forwarding on parent P2P bridges if any). +* This function will block if some conflicting card is already locking +* one of the required resources (or any resource on a different bus +* segment, since P2P bridges don't differenciate VGA memory and IO +* afaik). You can indicate wether this blocking should be interruptible +* by a signal (for userland interface) or not. +* Must not be called at interrupt time or in atomic context. +* If the card already owns the resources, the function succeeds. +* Nested calls are supported (a per-resource counter is maintained) +*/ + +extern int vga_get (struct pci_dev *pdev,unsigned int rsrc, +int interruptible); + +/** +* vga_get_interruptible +* +* Shortcut to vga_get
include/linux/vgaarb.h line 90
Quoted 10 lines
include/linux/vgaarb.h new file +10
-0+*/ + +static inline int vga_get_interruptible (struct pci_dev *pdev, +unsigned int rsrc) +{ +return vga_get (pdev, rsrc,1 ); +} + +/** +* vga_get_interruptible
Should be vga_get_uninterruptible.
Quoted 70 lines
include/linux/vgaarb.h new file +70
-0+* +* Shortcut to vga_get +*/ + +static inline int vga_get_uninterruptible (struct pci_dev *pdev, +unsigned int rsrc) +{ +return vga_get (pdev, rsrc,0 ); +} + +/** +* vga_tryget - try to acquire & lock legacy VGA resources +* +* @pdev: pci devivce of VGA card or NULL for system default +* @rsrc: bit mask of resources to acquire and lock +* +* This function performs the same operation as vga_get(), but +* will return an error (-EBUSY) instead of blocking if the resources +* are already locked by another card. It can be called in any context +*/ + +extern int vga_tryget (struct pci_dev *pdev,unsigned int rsrc); + +/** +* vga_put - release lock on legacy VGA resources +* +* @pdev: pci device of VGA card or NULL for system default +* @rsrc: but mask of resource to release +* +* This function releases resources previously locked by vga_get() +* or vga_tryget(). The resources aren't disabled right away, so +* that a subsequence vga_get() on the same card will succeed +* immediately. Resources have a counter, so locks are only +* released if the counter reaches 0. +*/ + +extern void vga_put (struct pci_dev *pdev,unsigned int rsrc); + + +/** +* vga_default_device +* +* This can be defined by the platform. The default implementation +* is rather dumb and will probably only work properly on single +* vga card setups and/or x86 platforms. +* +* If your VGA default device is not PCI, you'll have to return +* NULL here. In this case, I assume it will not conflict with +* any PCI card. If this is not true, I'll have to define two archs +* hooks for enabling/disabling the VGA default device if that is +* possible. This may be a problem with real _ISA_ VGA cards, in +* addition to a PCI one. I don't know at this point how to deal +* with that card. Can theirs IOs be disabled at all ? If not, then +* I suppose it's a matter of having the proper arch hook telling +* us about it, so we basically never allow anybody to succeed a +* vga_get()... +*/ + +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +extern struct pci_dev *vga_default_device (void ); +#endif + +/** +* vga_conflicts +* +* Architectures should define this if they have several +* independant PCI domains that can afford concurrent VGA +* decoding +*/ +
include/linux/vgaarb.h line 170
Quoted 10 lines
include/linux/vgaarb.h new file +10
-0+#ifndef __ARCH_HAS_VGA_CONFLICT +static inline int vga_conflicts (struct pci_dev *p1,struct pci_dev *p2) +{ +return 1 ; +} +#endif + +/* +* Register a client with the VGA arbitration logic +* return value: number of VGA devices in system.
The return value is documented incorrectly here.
Quoted 27 lines
include/linux/vgaarb.h new file +25
-0+* +* Clients have two callback mechanisms they can use. +* irq enable/disable callback - +* If a client can't disable its GPUs VGA resources, then we +* need to be able to ask it to turn off its irqs when we +* turn off its mem and io decoding. +* set_vga_decode +* If a client can disable its GPU VGA resource, it will +* get a callback from this to set the encode/decode state +* +* Clients with disable abilities should check the return value +* of this function and if the VGA device count is > 1, should +* disable VGA decoding resources. +* +* Rationale: we cannot disable VGA decode resources unconditionally +* some single GPU laptops seem to require ACPI or BIOS access to the +* VGA registers to control things like backlights etc. +* Hopefully newer multi-GPU laptops do something saner, and desktops +* won't have any special ACPI for this. +*/ +int vga_client_register (struct pci_dev *pdev,void *cookie, +void (*irq_set_state)(void *cookie,bool state), +unsigned int (*set_vga_decode)(void *cookie,bool state)); + +#endif /* LINUX_VGA_H */ Signature · 1 line
1.6.0.6
Signature · 2 lines
Pekka Paalanen http://www.iki.fi/pq/
Quoted 21 lines · On Tue, 11 Aug 2009 15:52:06 +1000 Dave Airlie <[email protected]> wrote:
From: Tiago Vignatti <[email protected]>
Background:
Graphic devices are accessed through ranges in I/O or memory space. While most modern devices allow relocation of such ranges, some "Legacy" VGA devices implemented on PCI will typically have the same "hard-decoded" addresses as they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware Revision 2.1" Section 7, Legacy Devices.The Resource Access Control (RAC) module inside the X server currently does the task of arbitration when more than one legacy device co-exists on the same machine. But the problem happens when these devices are trying to be accessed by different userspace clients (e.g. two server in parallel). Their address assignments conflict. Therefore an arbitration scheme _outside_ of the X server is needed to control the sharing of these resources. This document introduces the operation of the VGA arbiter implemented for Linux kernel.
Quoted 2 lines
Signed-off-by Tiago Vignatti <[email protected]>Signed-off-by Dave Airlie <[email protected]>
Ok, applied this to my linux-next branch, but I'd like to get Ben's s-o-b before pushing it to Linus.
Ben?
Thanks,
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
Quoted 24 lines · On Tue, 11 Aug 2009 16:17:46 -0700 Jesse Barnes <[email protected]> wrote:
Quoted 21 lines · On Tue, 11 Aug 2009 15:52:06 +1000 Dave Airlie <[email protected]> wrote:
From: Tiago Vignatti <[email protected]>
Background:
Graphic devices are accessed through ranges in I/O or memory space. While most modern devices allow relocation of such ranges, some "Legacy" VGA devices implemented on PCI will typically have the same "hard-decoded" addresses as they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware Revision 2.1" Section 7, Legacy Devices.The Resource Access Control (RAC) module inside the X server currently does the task of arbitration when more than one legacy device co-exists on the same machine. But the problem happens when these devices are trying to be accessed by different userspace clients (e.g. two server in parallel). Their address assignments conflict. Therefore an arbitration scheme _outside_ of the X server is needed to control the sharing of these resources. This document introduces the operation of the VGA arbiter implemented for Linux kernel.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Tiago Vignatti <[email protected]>Signed-off-by Dave Airlie <[email protected]>Ok, applied this to my linux-next branch, but I'd like to get Ben's s-o-b before pushing it to Linus.
Oh yeah, and we should get the documentation merged too. I can handle that or you can send it to Randy. Tiago?
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
Quoted 2 lines · Jesse Barnes
Ok, applied this to my linux-next branch, but I'd like to get Ben's s-o-b before pushing it to Linus.
Well, S-O-B is if the code went through my hands... though in this case I wrote the original version so I suppose it did :-) An ack for sure.
Let me have a look, I'll come back to you asap.
Cheers,
Ben.
Quoted 18 lines · Dave Airlie
From: Tiago Vignatti <[email protected]>
Background:
Graphic devices are accessed through ranges in I/O or memory space. While most modern devices allow relocation of such ranges, some "Legacy" VGA devices implemented on PCI will typically have the same "hard-decoded" addresses as they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware Revision 2.1" Section 7, Legacy Devices.The Resource Access Control (RAC) module inside the X server currently does the task of arbitration when more than one legacy device co-exists on the same machine. But the problem happens when these devices are trying to be accessed by different userspace clients (e.g. two server in parallel). Their address assignments conflict. Therefore an arbitration scheme _outside_ of the X server is needed to control the sharing of these resources. This document introduces the operation of the VGA arbiter implemented for Linux kernel.
Quoted 2 lines
Signed-off-by Tiago Vignatti <[email protected]>Signed-off-by Dave Airlie <[email protected]>
Well, since I wrote a god deal of it:
---
Note that I do believe we still have some kind of race vs. the default VGA device going away but it's rather minor, something to fix at some stase though.
Quoted 1,539 lines
--- drivers/gpu/Makefile | 2 +- drivers/gpu/vga/Kconfig | 10 + drivers/gpu/vga/Makefile | 1 + drivers/gpu/vga/vgaarb.c | 1206 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 44 ++ drivers/video/Kconfig | 2 + include/linux/pci.h | 2 + include/linux/vgaarb.h | 195 ++++++++ 8 files changed, 1461 insertions(+), 1 deletions(-) create mode 100644 drivers/gpu/vga/Kconfig create mode 100644 drivers/gpu/vga/Makefile create mode 100644 drivers/gpu/vga/vgaarb.c create mode 100644 include/linux/vgaarb.hdrivers/gpu/Makefile +1
-1@@ -1 +1 @@ -obj-y += drm/ +obj-y += drm/ vga/drivers/gpu/vga/Kconfig new file +10
-0@@ -0,0 +1,10 @@ +config VGA_ARB + bool "VGA Arbitration" if EMBEDDED + default y + depends on PCI + help + Some "legacy" VGA devices implemented on PCI typically have the same + hard-decoded addresses as they did on ISA. When multiple PCI devices + are accessed at same time they need some kind of coordination. Please + see Documentation/vgaarbiter.txt for more details. Select this to + enable VGA arbiter.drivers/gpu/vga/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-$(CONFIG_VGA_ARB) += vgaarb.odrivers/gpu/vga/vgaarb.c new file +1206
-0@@ -0,0 +1,1206 @@ +/* +* vgaarb.c +* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]> +* (C) Copyright 2007 Paulo R. Zanoni <[email protected]> +* (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]> +* +* Implements the VGA arbitration. For details refer to +* Documentation/vgaarbiter.txt +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/pci.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/wait.h> +#include <linux/spinlock.h> +#include <linux/poll.h> +#include <linux/miscdevice.h> + +#include <linux/uaccess.h> + +#include <linux/vgaarb.h> + +static void vga_arbiter_notify_clients (void ); +/* +* We keep a list of all vga devices in the system to speed +* up the various operations of the arbiter +*/ +struct vga_device { +struct list_head list; +struct pci_dev *pdev; +unsigned int decodes;/* what does it decodes */ +unsigned int owns;/* what does it owns */ +unsigned int locks;/* what does it locks */ +unsigned int io_lock_cnt;/* legacy IO lock count */ +unsigned int mem_lock_cnt;/* legacy MEM lock count */ +unsigned int io_norm_cnt;/* normal IO count */ +unsigned int mem_norm_cnt;/* normal MEM count */ + +/* allow IRQ enable/disable hook */ +void *cookie; +void (*irq_set_state)(void *cookie,bool enable); +unsigned int (*set_vga_decode)(void *cookie,bool decode); +}; + +static LIST_HEAD (vga_list); +static int vga_count, vga_decode_count; +static bool vga_arbiter_used; +static DEFINE_SPINLOCK (vga_lock); +static DECLARE_WAIT_QUEUE_HEAD (vga_wait_queue); + + +static const char *vga_iostate_to_str (unsigned int iostate) +{ +/* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ + iostate &=VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +switch (iostate) { +case VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM : +return "io+mem" ; +case VGA_RSRC_LEGACY_IO : +return "io" ; +case VGA_RSRC_LEGACY_MEM : +return "mem" ; + } +return "none" ; +} + +static int vga_str_to_iostate (char *buf,int str_size,int *io_state) +{ +/* we could in theory hand out locks on IO and mem +* separately to userspace but it can cause deadlocks */ +if (strncmp (buf,"none" ,4 ) ==0 ) { + *io_state =VGA_RSRC_NONE ; +return 1 ; + } + +/* XXX We're not chekcing the str_size! */ +if (strncmp (buf,"io+mem" ,6 ) ==0 ) + goto both; +else if (strncmp (buf,"io" ,2 ) ==0 ) + goto both; +else if (strncmp (buf,"mem" ,3 ) ==0 ) + goto both; +return 0 ; +both: + *io_state =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +return 1 ; +} + +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +/* this is only used a cookie - it should not be dereferenced */ +static struct pci_dev *vga_default; +#endif + +static void vga_arb_device_card_gone (struct pci_dev *pdev); + +/* Find somebody in our list */ +static struct vga_device *vgadev_find (struct pci_dev *pdev) +{ +struct vga_device *vgadev; + +list_for_each_entry (vgadev, &vga_list, list) +if (pdev == vgadev->pdev) +return vgadev; +return NULL ; +} + +/* Returns the default VGA device (vgacon's babe) */ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +struct pci_dev *vga_default_device (void ) +{ +return vga_default; +} +#endif + +static inline void vga_irq_set_state (struct vga_device *vgadev,bool state) +{ +if (vgadev->irq_set_state) + vgadev->irq_set_state (vgadev->cookie, state); +} + + +/* If we don't ever use VGA arb we should avoid +turning off anything anywhere due to old X servers getting +confused about the boot device not being VGA */ +static void vga_check_first_use (void ) +{ +/* we should inform all GPUs in the system that +* VGA arb has occured and to try and disable resources +* if they can */ +if (!vga_arbiter_used) { + vga_arbiter_used = true; +vga_arbiter_notify_clients (); + } +} + +static struct vga_device *__vga_tryget (struct vga_device *vgadev, +unsigned int rsrc) +{ +unsigned int wants, legacy_wants, match; +struct vga_device *conflict; +unsigned int pci_bits; +/* Account for "normal" resources to lock. If we decode the legacy, +* counterpart, we need to request it as well +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && + (vgadev->decodes &VGA_RSRC_LEGACY_IO )) + rsrc |=VGA_RSRC_LEGACY_IO ; +if ((rsrc &VGA_RSRC_NORMAL_MEM ) && + (vgadev->decodes &VGA_RSRC_LEGACY_MEM )) + rsrc |=VGA_RSRC_LEGACY_MEM ; + +pr_devel ("%s: %d\n" , __func__, rsrc); +pr_devel ("%s: owns: %d\n" , __func__, vgadev->owns); + +/* Check what resources we need to acquire */ + wants = rsrc & ~vgadev->owns; + +/* We already own everything, just mark locked & bye bye */ +if (wants ==0 ) + goto lock_them; + +/* We don't need to request a legacy resource, we just enable +* appropriate decoding and go +*/ + legacy_wants = wants &VGA_RSRC_LEGACY_MASK ; +if (legacy_wants ==0 ) + goto enable_them; + +/* Ok, we don't, let's find out how we need to kick off */ +list_for_each_entry (conflict, &vga_list, list) { +unsigned int lwants = legacy_wants; +unsigned int change_bridge =0 ; + +/* Don't conflict with myself */ +if (vgadev == conflict) +continue ; + +/* Check if the architecture allows a conflict between those +* 2 devices or if they are on separate domains +*/ +if (!vga_conflicts (vgadev->pdev, conflict->pdev)) +continue ; + +/* We have a possible conflict. before we go further, we must +* check if we sit on the same bus as the conflicting device. +* if we don't, then we must tie both IO and MEM resources +* together since there is only a single bit controlling +* VGA forwarding on P2P bridges +*/ +if (vgadev->pdev->bus != conflict->pdev->bus) { + change_bridge =1 ; + lwants =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; + } + +/* Check if the guy has a lock on the resource. If he does, +* return the conflicting entry +*/ +if (conflict->locks & lwants) +return conflict; + +/* Ok, now check if he owns the resource we want. We don't need +* to check "decodes" since it should be impossible to own +* own legacy resources you don't decode unless I have a bug +* in this code... +*/ +WARN_ON (conflict->owns & ~conflict->decodes); + match = lwants & conflict->owns; +if (!match) +continue ; + +/* looks like he doesn't have a lock, we can steal +* them from him +*/ +vga_irq_set_state (conflict, false); + + pci_bits =0 ; +if (lwants & (VGA_RSRC_LEGACY_MEM |VGA_RSRC_NORMAL_MEM )) + pci_bits |=PCI_COMMAND_MEMORY ; +if (lwants & (VGA_RSRC_LEGACY_IO |VGA_RSRC_NORMAL_IO )) + pci_bits |=PCI_COMMAND_IO ; + +pci_set_vga_state (conflict->pdev, false, pci_bits, + change_bridge); + conflict->owns &= ~lwants; +/* If he also owned non-legacy, that is no longer the case */ +if (lwants &VGA_RSRC_LEGACY_MEM ) + conflict->owns &= ~VGA_RSRC_NORMAL_MEM ; +if (lwants &VGA_RSRC_LEGACY_IO ) + conflict->owns &= ~VGA_RSRC_NORMAL_IO ; + } + +enable_them: +/* ok dude, we got it, everybody conflicting has been disabled, let's +* enable us. Make sure we don't mark a bit in "owns" that we don't +* also have in "decodes". We can lock resources we don't decode but +* not own them. +*/ + pci_bits =0 ; +if (wants & (VGA_RSRC_LEGACY_MEM |VGA_RSRC_NORMAL_MEM )) + pci_bits |=PCI_COMMAND_MEMORY ; +if (wants & (VGA_RSRC_LEGACY_IO |VGA_RSRC_NORMAL_IO )) + pci_bits |=PCI_COMMAND_IO ; +pci_set_vga_state (vgadev->pdev, true, pci_bits, !!(wants &VGA_RSRC_LEGACY_MASK )); + +vga_irq_set_state (vgadev, true); + vgadev->owns |= (wants & vgadev->decodes); +lock_them: + vgadev->locks |= (rsrc &VGA_RSRC_LEGACY_MASK ); +if (rsrc &VGA_RSRC_LEGACY_IO ) + vgadev->io_lock_cnt++; +if (rsrc &VGA_RSRC_LEGACY_MEM ) + vgadev->mem_lock_cnt++; +if (rsrc &VGA_RSRC_NORMAL_IO ) + vgadev->io_norm_cnt++; +if (rsrc &VGA_RSRC_NORMAL_MEM ) + vgadev->mem_norm_cnt++; + +return NULL ; +} + +static void __vga_put (struct vga_device *vgadev,unsigned int rsrc) +{ +unsigned int old_locks = vgadev->locks; + +pr_devel ("%s\n" , __func__); + +/* Update our counters, and account for equivalent legacy resources +* if we decode them +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && vgadev->io_norm_cnt >0 ) { + vgadev->io_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_IO ) + rsrc |=VGA_RSRC_LEGACY_IO ; + } +if ((rsrc &VGA_RSRC_NORMAL_MEM ) && vgadev->mem_norm_cnt >0 ) { + vgadev->mem_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_MEM ) + rsrc |=VGA_RSRC_LEGACY_MEM ; + } +if ((rsrc &VGA_RSRC_LEGACY_IO ) && vgadev->io_lock_cnt >0 ) + vgadev->io_lock_cnt--; +if ((rsrc &VGA_RSRC_LEGACY_MEM ) && vgadev->mem_lock_cnt >0 ) + vgadev->mem_lock_cnt--; + +/* Just clear lock bits, we do lazy operations so we don't really +* have to bother about anything else at this point +*/ +if (vgadev->io_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_IO ; +if (vgadev->mem_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_MEM ; + +/* Kick the wait queue in case somebody was waiting if we actually +* released something +*/ +if (old_locks != vgadev->locks) +wake_up_all (&vga_wait_queue); +} + +int vga_get (struct pci_dev *pdev,unsigned int rsrc,int interruptible) +{ +struct vga_device *vgadev, *conflict; +unsigned long flags; +wait_queue_t wait; +int rc =0 ; + +vga_check_first_use (); +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; + +for (;;) { +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +spin_unlock_irqrestore (&vga_lock, flags); + rc = -ENODEV ; +break ; + } + conflict =__vga_tryget (vgadev, rsrc); +spin_unlock_irqrestore (&vga_lock, flags); +if (conflict ==NULL ) +break ; + + +/* We have a conflict, we wait until somebody kicks the +* work queue. Currently we have one work queue that we +* kick each time some resources are released, but it would +* be fairly easy to have a per device one so that we only +* need to attach to the conflicting device +*/ +init_waitqueue_entry (&wait, current); +add_wait_queue (&vga_wait_queue, &wait); +set_current_state (interruptible ? +TASK_INTERRUPTIBLE : +TASK_UNINTERRUPTIBLE ); +if (signal_pending (current)) { + rc = -EINTR ; +break ; + } +schedule (); +remove_wait_queue (&vga_wait_queue, &wait); +set_current_state (TASK_RUNNING ); + } +return rc; +} +EXPORT_SYMBOL (vga_get); + +int vga_tryget (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; +int rc =0 ; + +vga_check_first_use (); + +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + rc = -ENODEV ; + goto bail; + } +if (__vga_tryget (vgadev, rsrc)) + rc = -EBUSY ; +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return rc; +} +EXPORT_SYMBOL (vga_tryget); + +void vga_put (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; + +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; +__vga_put (vgadev, rsrc); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +} +EXPORT_SYMBOL (vga_put); + +/* +* Currently, we assume that the "initial" setup of the system is +* not sane, that is we come up with conflicting devices and let +* the arbiter's client decides if devices decodes or not legacy +* things. +*/ +static bool vga_arbiter_add_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + +/* Only deal with VGA class devices */ +if ((pdev->class >>8 ) !=PCI_CLASS_DISPLAY_VGA ) +return false; + +/* Allocate structure */ + vgadev =kmalloc (sizeof (struct vga_device ),GFP_KERNEL ); +if (vgadev ==NULL ) { +pr_err ("vgaarb: failed to allocate pci device\n" ); +/* What to do on allocation failure ? For now, let's +* just do nothing, I'm not sure there is anything saner +* to be done +*/ +return false; + } + +memset (vgadev,0 ,sizeof (*vgadev)); + +/* Take lock & check for duplicates */ +spin_lock_irqsave (&vga_lock, flags); +if (vgadev_find (pdev) !=NULL ) { +BUG_ON (1 ); + goto fail; + } + vgadev->pdev = pdev; + +/* By default, assume we decode everything */ + vgadev->decodes =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM | +VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; + +/* by default mark it as decoding */ + vga_decode_count++; +/* Mark that we "own" resources based on our enables, we will +* clear that below if the bridge isn't forwarding +*/ +pci_read_config_word (pdev,PCI_COMMAND , &cmd); +if (cmd &PCI_COMMAND_IO ) + vgadev->owns |=VGA_RSRC_LEGACY_IO ; +if (cmd &PCI_COMMAND_MEMORY ) + vgadev->owns |=VGA_RSRC_LEGACY_MEM ; + +/* Check if VGA cycles can get down to us */ + bus = pdev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { +u16 l; +pci_read_config_word (bridge,PCI_BRIDGE_CONTROL , + &l); +if (!(l &PCI_BRIDGE_CTL_VGA )) { + vgadev->owns =0 ; +break ; + } + } + bus = bus->parent; + } + +/* Deal with VGA default device. Use first enabled one +* by default if arch doesn't have it's own hook +*/ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +if (vga_default ==NULL && + ((vgadev->owns &VGA_RSRC_LEGACY_MASK ) ==VGA_RSRC_LEGACY_MASK )) + vga_default =pci_dev_get (pdev); +#endif + +/* Add to the list */ +list_add (&vgadev->list, &vga_list); + vga_count++; +pr_info ("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n" , +pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), +vga_iostate_to_str (vgadev->locks)); + +spin_unlock_irqrestore (&vga_lock, flags); +return true; +fail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return false; +} + +static bool vga_arbiter_del_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +bool ret = true; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + ret = false; + goto bail; + } + +if (vga_default == pdev) { +pci_dev_put (vga_default); + vga_default =NULL ; + } + +if (vgadev->decodes & (VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM )) + vga_decode_count--; + +/* Remove entry from list */ +list_del (&vgadev->list); + vga_count--; +/* Notify userland driver that the device is gone so it discards +* it's copies of the pci_dev pointer +*/ +vga_arb_device_card_gone (pdev); + +/* Wake up all possible waiters */ +wake_up_all (&vga_wait_queue); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return ret; +} + +/* this is called with the lock */ +static inline void vga_update_device_decodes (struct vga_device *vgadev, +int new_decodes) +{ +int old_decodes; +struct vga_device *new_vgadev, *conflict; + + old_decodes = vgadev->decodes; + vgadev->decodes = new_decodes; + +pr_info ("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n" , +pci_name (vgadev->pdev), +vga_iostate_to_str (old_decodes), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns)); + + +/* if we own the decodes we should move them along to +another card */ +if ((vgadev->owns & old_decodes) && (vga_count >1 )) { +/* set us to own nothing */ + vgadev->owns &= ~old_decodes; +list_for_each_entry (new_vgadev, &vga_list, list) { +if ((new_vgadev != vgadev) && + (new_vgadev->decodes &VGA_RSRC_LEGACY_MASK )) { +pr_info ("vgaarb: transferring owner from PCI:%s to PCI:%s\n" ,pci_name (vgadev->pdev),pci_name (new_vgadev->pdev)); + conflict =__vga_tryget (new_vgadev,VGA_RSRC_LEGACY_MASK ); +if (!conflict) +__vga_put (new_vgadev,VGA_RSRC_LEGACY_MASK ); +break ; + } + } + } + +/* change decodes counter */ +if (old_decodes != new_decodes) { +if (new_decodes & (VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM )) + vga_decode_count++; +else + vga_decode_count--; + } +} + +void __vga_set_legacy_decoding (struct pci_dev *pdev,unsigned int decodes,bool userspace) +{ +struct vga_device *vgadev; +unsigned long flags; + + decodes &=VGA_RSRC_LEGACY_MASK ; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; + +/* don't let userspace futz with kernel driver decodes */ +if (userspace && vgadev->set_vga_decode) + goto bail; + +/* update the device decodes + counter */ +vga_update_device_decodes (vgadev, decodes); + +/* XXX if somebody is going from "doesn't decode" to "decodes" state +* here, additional care must be taken as we may have pending owner +* ship of non-legacy region ... +*/ +bail: +spin_unlock_irqrestore (&vga_lock, flags); +} + +void vga_set_legacy_decoding (struct pci_dev *pdev,unsigned int decodes) +{ +__vga_set_legacy_decoding (pdev, decodes, false); +} +EXPORT_SYMBOL (vga_set_legacy_decoding); + +/* return number of active VGA devices */ +/* call with NULL to unregister */ +int vga_client_register (struct pci_dev *pdev,void *cookie, +void (*irq_set_state)(void *cookie,bool state), +unsigned int (*set_vga_decode)(void *cookie,bool decode)) +{ +int ret =-1 ; +struct vga_device *vgadev; +unsigned long flags; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (!vgadev) + goto bail; + + vgadev->irq_set_state = irq_set_state; + vgadev->set_vga_decode = set_vga_decode; + vgadev->cookie = cookie; + ret =0 ; + +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return ret; + +} +EXPORT_SYMBOL (vga_client_register); + +/* +* Char driver implementation +* +* Semantics is: +* +* open : open user instance of the arbitrer. by default, it's +* attached to the default VGA device of the system. +* +* close : close user instance, release locks +* +* read : return a string indicating the status of the target. +* an IO state string is of the form {io,mem,io+mem,none}, +* mc and ic are respectively mem and io lock counts (for +* debugging/diagnostic only). "decodes" indicate what the +* card currently decodes, "owns" indicates what is currently +* enabled on it, and "locks" indicates what is locked by this +* card. If the card is unplugged, we get "invalid" then for +* card_ID and an -ENODEV error is returned for any command +* until a new card is targeted +* +* "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)" +* +* write : write a command to the arbiter. List of commands is: +* +* target <card_ID> : switch target to card <card_ID> (see below) +* lock <io_state> : acquires locks on target ("none" is invalid io_state) +* trylock <io_state> : non-blocking acquire locks on target +* unlock <io_state> : release locks on target +* unlock all : release all locks on target held by this user +* decodes <io_state> : set the legacy decoding attributes for the card +* +* poll : event if something change on any card (not just the target) +* +* card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default" +* to go back to the system default card (TODO: not implemented yet). +* Currently, only PCI is supported as a prefix, but the userland API may +* support other bus types in the future, even if the current kernel +* implementation doesn't. +* +* Note about locks: +* +* The driver keeps track of which user has what locks on which card. It +* supports stacking, like the kernel one. This complexifies the implementation +* a bit, but makes the arbiter more tolerant to userspace problems and able +* to properly cleanup in all cases when a process dies. +* Currently, a max of 16 cards simultaneously can have locks issued from +* userspace for a given user (file descriptor instance) of the arbiter. +* +* If the device is hot-unplugged, there is a hook inside the module to notify +* they being added/removed in the system and automatically added/removed in +* the arbiter. +*/ + +#define MAX_USER_CARDS 16 +#define PCI_INVALID_CARD ((struct pci_dev *)-1UL) + +/* +* Each user has an array of these, tracking which cards have locks +*/ +struct vga_arb_user_card { +struct pci_dev *pdev; +unsigned int mem_cnt; +unsigned int io_cnt; +}; + +struct vga_arb_private { +struct list_head list; +struct pci_dev *target; +struct vga_arb_user_card cards[MAX_USER_CARDS ]; +spinlock_t lock; +}; + +static LIST_HEAD (vga_user_list); +static DEFINE_SPINLOCK (vga_user_lock); + + +/* +* This function gets a string in the format: "PCI:domain:bus:dev.fn" and +* returns the respective values. If the string is not in this format, +* it returns 0. +*/ +static int vga_pci_str_to_vars (char *buf,int count,unsigned int *domain, +unsigned int *bus,unsigned int *devfn) +{ +int n; +unsigned int slot, func; + + + n =sscanf (buf,"PCI:%x:%x:%x.%x" , domain, bus, &slot, &func); +if (n !=4 ) +return 0 ; + + *devfn =PCI_DEVFN (slot, func); + +return 1 ; +} + +static ssize_t vga_arb_read (struct file *file,char __user * buf, +size_t count,loff_t *ppos) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_device *vgadev; +struct pci_dev *pdev; +unsigned long flags; +size_t len; +int rc; +char *lbuf; + + lbuf =kmalloc (1024 ,GFP_KERNEL ); +if (lbuf ==NULL ) +return -ENOMEM ; + +/* Shields against vga_arb_device_card_gone (pci_dev going +* away), and allows access to vga list +*/ +spin_lock_irqsave (&vga_lock, flags); + +/* If we are targetting the default, use it */ + pdev = priv->target; +if (pdev ==NULL || pdev ==PCI_INVALID_CARD ) { +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Find card vgadev structure */ + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +/* Wow, it's not in the list, that shouldn't happen, +* let's fix us up and return invalid card +*/ +if (pdev == priv->target) +vga_arb_device_card_gone (pdev); +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Fill the buffer with infos */ + len =snprintf (lbuf,1024 , +"count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n" , + vga_decode_count,pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), +vga_iostate_to_str (vgadev->locks), + vgadev->io_lock_cnt, vgadev->mem_lock_cnt); + +spin_unlock_irqrestore (&vga_lock, flags); +done: + +/* Copy that to user */ +if (len > count) + len = count; + rc =copy_to_user (buf, lbuf, len); +kfree (lbuf); +if (rc) +return -EFAULT ; +return len; +} + +/* +* TODO: To avoid parsing inside kernel and to improve the speed we may +* consider use ioctl here +*/ +static ssize_t vga_arb_write (struct file *file,const char __user * buf, +size_t count,loff_t *ppos) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc =NULL ; +struct pci_dev *pdev; + +unsigned int io_state; + +char *kbuf, *curr_pos; +size_t remaining = count; + +int ret_val; +int i; + + + kbuf =kmalloc (count +1 ,GFP_KERNEL ); +if (!kbuf) +return -ENOMEM ; + +if (copy_from_user (kbuf, buf, count)) { +kfree (kbuf); +return -EFAULT ; + } + curr_pos = kbuf; + kbuf[count] ='\0' ;/* Just to make sure... */ + +if (strncmp (curr_pos,"lock " ,5 ) ==0 ) { + curr_pos +=5 ; + remaining -=5 ; + +pr_devel ("client 0x%X called 'lock'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +if (io_state ==VGA_RSRC_NONE ) { + ret_val = -EPROTO ; + goto done; + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +vga_get_uninterruptible (pdev, io_state); + +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { +if (io_state &VGA_RSRC_LEGACY_IO ) + priv->cards[i].io_cnt++; +if (io_state &VGA_RSRC_LEGACY_MEM ) + priv->cards[i].mem_cnt++; +break ; + } + } + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"unlock " ,7 ) ==0 ) { + curr_pos +=7 ; + remaining -=7 ; + +pr_devel ("client 0x%X called 'unlock'\n" , (int )priv); + +if (strncmp (curr_pos,"all" ,3 ) ==0 ) + io_state =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +else { +if (!vga_str_to_iostate + (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) + uc = &priv->cards[i]; + } + +if (!uc) +return -EINVAL ; + +if (io_state &VGA_RSRC_LEGACY_IO && uc->io_cnt ==0 ) +return -EINVAL ; + +if (io_state &VGA_RSRC_LEGACY_MEM && uc->mem_cnt ==0 ) +return -EINVAL ; + +vga_put (pdev, io_state); + +if (io_state &VGA_RSRC_LEGACY_IO ) + uc->io_cnt--; +if (io_state &VGA_RSRC_LEGACY_MEM ) + uc->mem_cnt--; + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"trylock " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; + +pr_devel ("client 0x%X called 'trylock'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +if (vga_tryget (pdev, io_state)) { +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { +if (io_state &VGA_RSRC_LEGACY_IO ) + priv->cards[i].io_cnt++; +if (io_state &VGA_RSRC_LEGACY_MEM ) + priv->cards[i].mem_cnt++; +break ; + } + } + ret_val = count; + goto done; + }else { + ret_val = -EBUSY ; + goto done; + } + + }else if (strncmp (curr_pos,"target " ,7 ) ==0 ) { +unsigned int domain, bus, devfn; +struct vga_device *vgadev; + + curr_pos +=7 ; + remaining -=7 ; +pr_devel ("client 0x%X called 'target'\n" , (int )priv); +/* if target is default */ +if (!strncmp (buf,"default" ,7 )) + pdev =pci_dev_get (vga_default_device ()); +else { +if (!vga_pci_str_to_vars (curr_pos, remaining, + &domain, &bus, &devfn)) { + ret_val = -EPROTO ; + goto done; + } + + pdev =pci_get_bus_and_slot (bus, devfn); +if (!pdev) { +pr_info ("vgaarb: invalid PCI address!\n" ); + ret_val = -ENODEV ; + goto done; + } + } + + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +pr_info ("vgaarb: this pci device is not a vga device\n" ); +pci_dev_put (pdev); + ret_val = -ENODEV ; + goto done; + } + + priv->target = pdev; +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) +break ; +if (priv->cards[i].pdev ==NULL ) { + priv->cards[i].pdev = pdev; + priv->cards[i].io_cnt =0 ; + priv->cards[i].mem_cnt =0 ; +break ; + } + } +if (i ==MAX_USER_CARDS ) { +pr_err ("vgaarb: maximum user cards number reached!\n" ); +pci_dev_put (pdev); +/* XXX: which value to return? */ + ret_val = -ENOMEM ; + goto done; + } + + ret_val = count; +pci_dev_put (pdev); + goto done; + + + }else if (strncmp (curr_pos,"decodes " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; +pr_devel ("vgaarb: client 0x%X called 'decodes'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +__vga_set_legacy_decoding (pdev, io_state, true); + ret_val = count; + goto done; + } +/* If we got here, the message written is not part of the protocol! */ +kfree (kbuf); +return -EPROTO ; + +done: +kfree (kbuf); +return ret_val; +} + +static unsigned int vga_arb_fpoll (struct file *file,poll_table * wait) +{ +struct vga_arb_private *priv = file->private_data; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; +poll_wait (file, &vga_wait_queue, wait); +return POLLIN ; +} + +static int vga_arb_open (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv; +unsigned long flags; + +pr_devel ("%s\n" , __func__); + + priv =kmalloc (sizeof (struct vga_arb_private ),GFP_KERNEL ); +if (priv ==NULL ) +return -ENOMEM ; +memset (priv,0 ,sizeof (*priv)); +spin_lock_init (&priv->lock); + file->private_data = priv; + +spin_lock_irqsave (&vga_user_lock, flags); +list_add (&priv->list, &vga_user_list); +spin_unlock_irqrestore (&vga_user_lock, flags); + +/* Set the client' lists of locks */ + priv->target =vga_default_device ();/* Maybe this is still null! */ + priv->cards[0 ].pdev = priv->target; + priv->cards[0 ].io_cnt =0 ; + priv->cards[0 ].mem_cnt =0 ; + + +return 0 ; +} + +static int vga_arb_release (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc; +unsigned long flags; +int i; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; + +spin_lock_irqsave (&vga_user_lock, flags); +list_del (&priv->list); +for (i =0 ; i <MAX_USER_CARDS ; i++) { + uc = &priv->cards[i]; +if (uc->pdev ==NULL ) +continue ; +pr_devel ("uc->io_cnt == %d, uc->mem_cnt == %d\n" , + uc->io_cnt, uc->mem_cnt); +while (uc->io_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_IO ); +while (uc->mem_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_MEM ); + } +spin_unlock_irqrestore (&vga_user_lock, flags); + +kfree (priv); + +return 0 ; +} + +static void vga_arb_device_card_gone (struct pci_dev *pdev) +{ +} + +/* +* callback any registered clients to let them know we have a +* change in VGA cards +*/ +static void vga_arbiter_notify_clients (void ) +{ +struct vga_device *vgadev; +unsigned long flags; +uint32_t new_decodes; +bool new_state; + +if (!vga_arbiter_used) +return ; + +spin_lock_irqsave (&vga_lock, flags); +list_for_each_entry (vgadev, &vga_list, list) { +if (vga_count >1 ) + new_state = false; +else + new_state = true; +if (vgadev->set_vga_decode) { + new_decodes = vgadev->set_vga_decode (vgadev->cookie, new_state); +vga_update_device_decodes (vgadev, new_decodes); + } + } +spin_unlock_irqrestore (&vga_lock, flags); +} + +static int pci_notify (struct notifier_block *nb,unsigned long action, +void *data) +{ +struct device *dev = data; +struct pci_dev *pdev =to_pci_dev (dev); +bool notify = false; + +pr_devel ("%s\n" , __func__); + +/* For now we're only intereted in devices added and removed. I didn't +* test this thing here, so someone needs to double check for the +* cases of hotplugable vga cards. */ +if (action ==BUS_NOTIFY_ADD_DEVICE ) + notify =vga_arbiter_add_pci_device (pdev); +else if (action ==BUS_NOTIFY_DEL_DEVICE ) + notify =vga_arbiter_del_pci_device (pdev); + +if (notify) +vga_arbiter_notify_clients (); +return 0 ; +} + +static struct notifier_block pci_notifier = { + .notifier_call = pci_notify, +}; + +static const struct file_operations vga_arb_device_fops = { + .read = vga_arb_read, + .write = vga_arb_write, + .poll = vga_arb_fpoll, + .open = vga_arb_open, + .release = vga_arb_release, +}; + +static struct miscdevice vga_arb_device = { +MISC_DYNAMIC_MINOR ,"vga_arbiter" , &vga_arb_device_fops +}; + +static int __init vga_arb_device_init (void ) +{ +int rc; +struct pci_dev *pdev; + + rc =misc_register (&vga_arb_device); +if (rc <0 ) +pr_err ("vgaarb: error %d registering device\n" , rc); + +bus_register_notifier (&pci_bus_type, &pci_notifier); + +/* We add all pci devices satisfying vga class in the arbiter by +* default */ + pdev =NULL ; +while ((pdev = +pci_get_subsys (PCI_ANY_ID ,PCI_ANY_ID ,PCI_ANY_ID , +PCI_ANY_ID , pdev)) !=NULL ) +vga_arbiter_add_pci_device (pdev); + +pr_info ("vgaarb: loaded\n" ); +return rc; +} +subsys_initcall (vga_arb_device_init);drivers/pci/pci.c +44
-0@@ -2502,6 +2502,50 @@ int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)return 0 ; } +/** +* pci_set_vga_state - set VGA decode state on device and parents if requested +* @dev the PCI device +* @decode - true = enable decoding, false = disable decoding +* @command_bits PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY +* @change_bridge - traverse ancestors and change bridges +*/ +int pci_set_vga_state (struct pci_dev *dev,bool decode, +unsigned int command_bits,bool change_bridge) +{ +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + +WARN_ON (command_bits & ~(PCI_COMMAND_IO |PCI_COMMAND_MEMORY )); + +pci_read_config_word (dev,PCI_COMMAND , &cmd); +if (decode == true) + cmd |= command_bits; +else + cmd &= ~command_bits; +pci_write_config_word (dev,PCI_COMMAND , cmd); + +if (change_bridge == false) +return 0 ; + + bus = dev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { +pci_read_config_word (bridge,PCI_BRIDGE_CONTROL , + &cmd); +if (decode == true) + cmd |=PCI_BRIDGE_CTL_VGA ; +else + cmd &= ~PCI_BRIDGE_CTL_VGA ; +pci_write_config_word (bridge,PCI_BRIDGE_CONTROL , + cmd); + } + bus = bus->parent; + } +return 0 ; +} +#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZEstatic char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE ] = {0 };spinlock_t resource_alignment_lock =SPIN_LOCK_UNLOCKED ;drivers/video/Kconfig +2
-0@@ -7,6 +7,8 @@ menu "Graphics support" source "drivers/char/agp/Kconfig" +source "drivers/gpu/vga/Kconfig" + source "drivers/gpu/drm/Kconfig" config VGASTATEinclude/linux/pci.h +2
-0@@ -805,6 +805,8 @@ int pci_cfg_space_size_ext(struct pci_dev *dev);int pci_cfg_space_size (struct pci_dev *dev);unsigned char pci_bus_max_busnr (struct pci_bus *bus); +int pci_set_vga_state (struct pci_dev *pdev,bool decode, +unsigned int command_bits,bool change_bridge);/* kmem_cache style wrapper around pci_alloc_consistent() */ #include <linux/dmapool.h> include/linux/vgaarb.h new file +195
-0@@ -0,0 +1,195 @@ +/* +* vgaarb.c +* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]> +* (C) Copyright 2007 Paulo R. Zanoni <[email protected]> +* (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]> +*/ + +#ifndef LINUX_VGA_H + +#include <asm/vga.h> + +/* Legacy VGA regions */ +#define VGA_RSRC_NONE 0x00 +#define VGA_RSRC_LEGACY_IO 0x01 +#define VGA_RSRC_LEGACY_MEM 0x02 +#define VGA_RSRC_LEGACY_MASK (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM) +/* Non-legacy access */ +#define VGA_RSRC_NORMAL_IO 0x04 +#define VGA_RSRC_NORMAL_MEM 0x08 + +/* Passing that instead of a pci_dev to use the system "default" +* device, that is the one used by vgacon. Archs will probably +* have to provide their own vga_default_device(); +*/ +#define VGA_DEFAULT_DEVICE (NULL) + +/* For use by clients */ + +/** +* vga_set_legacy_decoding +* +* @pdev: pci device of the VGA card +* @decodes: bit mask of what legacy regions the card decodes +* +* Indicates to the arbiter if the card decodes legacy VGA IOs, +* legacy VGA Memory, both, or none. All cards default to both, +* the card driver (fbdev for example) should tell the arbiter +* if it has disabled legacy decoding, so the card can be left +* out of the arbitration process (and can be safe to take +* interrupts at any time. +*/ +extern void vga_set_legacy_decoding (struct pci_dev *pdev, +unsigned int decodes); + +/** +* vga_get - acquire & locks VGA resources +* +* pdev: pci device of the VGA card or NULL for the system default +* rsrc: bit mask of resources to acquire and lock +* interruptible: blocking should be interruptible by signals ? +* +* This function acquires VGA resources for the given +* card and mark those resources locked. If the resource requested +* are "normal" (and not legacy) resources, the arbiter will first check +* wether the card is doing legacy decoding for that type of resource. If +* yes, the lock is "converted" into a legacy resource lock. +* The arbiter will first look for all VGA cards that might conflict +* and disable their IOs and/or Memory access, inlcuding VGA forwarding +* on P2P bridges if necessary, so that the requested resources can +* be used. Then, the card is marked as locking these resources and +* the IO and/or Memory accesse are enabled on the card (including +* VGA forwarding on parent P2P bridges if any). +* This function will block if some conflicting card is already locking +* one of the required resources (or any resource on a different bus +* segment, since P2P bridges don't differenciate VGA memory and IO +* afaik). You can indicate wether this blocking should be interruptible +* by a signal (for userland interface) or not. +* Must not be called at interrupt time or in atomic context. +* If the card already owns the resources, the function succeeds. +* Nested calls are supported (a per-resource counter is maintained) +*/ + +extern int vga_get (struct pci_dev *pdev,unsigned int rsrc, +int interruptible); + +/** +* vga_get_interruptible +* +* Shortcut to vga_get +*/ + +static inline int vga_get_interruptible (struct pci_dev *pdev, +unsigned int rsrc) +{ +return vga_get (pdev, rsrc,1 ); +} + +/** +* vga_get_interruptible +* +* Shortcut to vga_get +*/ + +static inline int vga_get_uninterruptible (struct pci_dev *pdev, +unsigned int rsrc) +{ +return vga_get (pdev, rsrc,0 ); +} + +/** +* vga_tryget - try to acquire & lock legacy VGA resources +* +* @pdev: pci devivce of VGA card or NULL for system default +* @rsrc: bit mask of resources to acquire and lock +* +* This function performs the same operation as vga_get(), but +* will return an error (-EBUSY) instead of blocking if the resources +* are already locked by another card. It can be called in any context +*/ + +extern int vga_tryget (struct pci_dev *pdev,unsigned int rsrc); + +/** +* vga_put - release lock on legacy VGA resources +* +* @pdev: pci device of VGA card or NULL for system default +* @rsrc: but mask of resource to release +* +* This function releases resources previously locked by vga_get() +* or vga_tryget(). The resources aren't disabled right away, so +* that a subsequence vga_get() on the same card will succeed +* immediately. Resources have a counter, so locks are only +* released if the counter reaches 0. +*/ + +extern void vga_put (struct pci_dev *pdev,unsigned int rsrc); + + +/** +* vga_default_device +* +* This can be defined by the platform. The default implementation +* is rather dumb and will probably only work properly on single +* vga card setups and/or x86 platforms. +* +* If your VGA default device is not PCI, you'll have to return +* NULL here. In this case, I assume it will not conflict with +* any PCI card. If this is not true, I'll have to define two archs +* hooks for enabling/disabling the VGA default device if that is +* possible. This may be a problem with real _ISA_ VGA cards, in +* addition to a PCI one. I don't know at this point how to deal +* with that card. Can theirs IOs be disabled at all ? If not, then +* I suppose it's a matter of having the proper arch hook telling +* us about it, so we basically never allow anybody to succeed a +* vga_get()... +*/ + +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +extern struct pci_dev *vga_default_device (void ); +#endif + +/** +* vga_conflicts +* +* Architectures should define this if they have several +* independant PCI domains that can afford concurrent VGA +* decoding +*/ + +#ifndef __ARCH_HAS_VGA_CONFLICT +static inline int vga_conflicts (struct pci_dev *p1,struct pci_dev *p2) +{ +return 1 ; +} +#endif + +/* +* Register a client with the VGA arbitration logic +* return value: number of VGA devices in system. +* +* Clients have two callback mechanisms they can use. +* irq enable/disable callback - +* If a client can't disable its GPUs VGA resources, then we +* need to be able to ask it to turn off its irqs when we +* turn off its mem and io decoding. +* set_vga_decode +* If a client can disable its GPU VGA resource, it will +* get a callback from this to set the encode/decode state +* +* Clients with disable abilities should check the return value +* of this function and if the VGA device count is > 1, should +* disable VGA decoding resources. +* +* Rationale: we cannot disable VGA decode resources unconditionally +* some single GPU laptops seem to require ACPI or BIOS access to the +* VGA registers to control things like backlights etc. +* Hopefully newer multi-GPU laptops do something saner, and desktops +* won't have any special ACPI for this. +*/ +int vga_client_register (struct pci_dev *pdev,void *cookie, +void (*irq_set_state)(void *cookie,bool state), +unsigned int (*set_vga_decode)(void *cookie,bool state)); + +#endif /* LINUX_VGA_H */
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 2 lines · Jesse Barnes
Ok, applied this to my linux-next branch, but I'd like to get Ben's s-o-b before pushing it to Linus.
Well, S-O-B is if the code went through my hands... though in this case I wrote the original version so I suppose it did :-) An ack for sure.
Let me have a look, I'll come back to you asap.
Ohh, I feel more comfortable if the patches come with Ben's authorship. Please do it Dave because it makes more sense (and S-O-B by me).
Cheers,
Tiago
Quoted 2 lines · Jesse Barnes
Oh yeah, and we should get the documentation merged too. I can handle that or you can send it to Randy. Tiago?
Okay, I can handle this documentation pretty easy.
But honestly I'm not sure we (or some future developer) will use it. The header (include/linux/vgaarb.h) is already very nice documented. So, tell me what do you think Jesse.
Cheers,
Tiago
Quoted 11 lines · On Wed, 12 Aug 2009 00:24:37 -0700 "Vignatti Tiago (Nokia-D/Helsinki)"…
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 2 lines · Jesse Barnes
Ok, applied this to my linux-next branch, but I'd like to get Ben's s-o-b before pushing it to Linus.
Well, S-O-B is if the code went through my hands... though in this case I wrote the original version so I suppose it did :-) An ack for sure.
Quoted 5 lines
Quoted 1 line
Let me have a look, I'll come back to you asap.
Ohh, I feel more comfortable if the patches come with Ben's authorship. Please do it Dave because it makes more sense (and S-O-B by me).
Ok I amended the patch to make Ben the author, with s-o-bs from Dave and you since you both worked on the code.
Dave, do you want me to apply the DRM patch too, just to avoid having a dependency? You could also pull my tree into yours; let me know if you do and I won't do anymore rebases before sending Linus a pull request.
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
Quoted 9 lines · On Wed, 12 Aug 2009 10:35:16 +0300 "Vignatti Tiago (Nokia-D/Helsinki)"…
Quoted 2 lines · Jesse Barnes
Oh yeah, and we should get the documentation merged too. I can handle that or you can send it to Randy. Tiago?
Okay, I can handle this documentation pretty easy.
But honestly I'm not sure we (or some future developer) will use it. The header (
include/linux/vgaarb.h) is already very nice documented. So, tell me what do you think Jesse.
As long as it's still accurate we may as well include it.
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
Quoted 9 lines · Jesse Barnes
Quoted 5 lines · On Wed, 12 Aug 2009 10:35:16 +0300 "Vignatti Tiago (Nokia-D/Helsinki)"…
Okay, I can handle this documentation pretty easy.
But honestly I'm not sure we (or some future developer) will use it. The header (
include/linux/vgaarb.h) is already very nice documented. So, tell me what do you think Jesse.As long as it's still accurate we may as well include it.
Here it comes attached. Please review, Jesse.
Thank you,
Tiago
Quoted 1 line
From
f65764e41e37c9a4848bb39505e40ea55e01ddfbMon Sep 17 00:00:00 2001
From: Tiago Vignatti <[email protected]>
Date: Sun, 16 Aug 2009 17:59:01 +0300
Subject: [PATCH] vga: drops VGA arbitration documentation
--- Documentation/vgaarbiter.txt | 194 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 194 insertions(+), 0 deletions(-) create mode 100644 Documentation/vgaarbiter.txt
Documentation/vgaarbiter.txt new file +194 -0
@@ -0,0 +1,194 @@ + +VGA Arbiter +=========== + +Graphic devices are accessed through ranges in I/O or memory space. While most +modern devices allow relocation of such ranges, some "Legacy" VGA devices +implemented on PCI will typically have the same "hard-decoded" addresses as +they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 +Standard for Boot (Initialization Configuration) Firmware Revision 2.1" +Section 7, Legacy Devices. + +The Resource Access Control (RAC) module inside the X server [0] existed for +the legacy VGA arbitration task (besides other bus management tasks) when more +than one legacy device co-exists on the same machine. But the problem happens +when these devices are trying to be accessed by different userspace clients +(e.g. two server in parallel). Their address assignments conflict. Moreover, +ideally, being an userspace application, it is not the role of the the X +server to control bus resources. Therefore an arbitration scheme outside of +the X server is needed to control the sharing of these resources. This +document introduces the operation of the VGA arbiter implemented for Linux +kernel. + +---------------------------------------------------------------------------- + +I. Details and Theory of Operation + I.1 vgaarb + I.2 libpciaccess + I.3 xf86VGAArbiter (X server implementation) +II. Credits +III.References + + +I. Details and Theory of Operation +================================== + +I.1 vgaarb +---------- + +The vgaarb is a module of the Linux Kernel. When it is initially loaded, it +scans all PCI devices and adds the VGA ones inside the arbitration. The +arbiter then enables/disables the decoding on different devices of the VGA +legacy instructions. Device which do not want/need to use the arbiter may +explicitly tell it by calling vga_set_legacy_decoding(). + +The kernel exports a char device interface (/dev/vga_arbiter) to the clients, +which has the following semantics: + + open : open user instance of the arbiter. By default, it's attached to + the default VGA device of the system. + + close : close user instance. Release locks made by the user + + read : return a string indicating the status of the target like: + + "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)" + + An IO state string is of the form {io,mem,io+mem,none}, mc and + ic are respectively mem and io lock counts (for debugging/ + diagnostic only). "decodes" indicate what the card currently + decodes, "owns" indicates what is currently enabled on it, and + "locks" indicates what is locked by this card. If the card is + unplugged, we get "invalid" then for card_ID and an -ENODEV + error is returned for any command until a new card is targeted. + + + write : write a command to the arbiter. List of commands: + + target <card_ID> : switch target to card <card_ID> (see below) + lock <io_state> : acquires locks on target ("none" is an invalid io_state) + trylock <io_state> : non-blocking acquire locks on target (returns EBUSY if + unsuccessful) + unlock <io_state> : release locks on target + unlock all : release all locks on target held by this user (not + implemented yet) + decodes <io_state> : set the legacy decoding attributes for the card + + poll : event if something changes on any card (not just the + target) + + card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default" + to go back to the system default card (TODO: not implemented yet). Currently, + only PCI is supported as a prefix, but the userland API may support other bus + types in the future, even if the current kernel implementation doesn't. + +Note about locks: + +The driver keeps track of which user has which locks on which card. It +supports stacking, like the kernel one. This complexifies the implementation +a bit, but makes the arbiter more tolerant to user space problems and able +to properly cleanup in all cases when a process dies. +Currently, a max of 16 cards can have locks simultaneously issued from +user space for a given user (file descriptor instance) of the arbiter. + +In the case of devices hot-{un,}plugged, there is a hook - pci_notify() - to +notify them being added/removed in the system and automatically added/removed +in the arbiter. + +There's also a in-kernel API of the arbiter in the case of DRM, vgacon and +others which may use the arbiter. + + +I.2 libpciaccess +---------------- + +To use the vga arbiter char device it was implemented an API inside the +libpciaccess library. One fieldd was added to struct pci_device (each device +on the system): + + /* the type of resource decoded by the device */ + int vgaarb_rsrc; + +Besides it, in pci_system were added: + + int vgaarb_fd; + int vga_count; + struct pci_device *vga_target; + struct pci_device *vga_default_dev; + + +The vga_count is usually need to keep informed how many cards are being +arbitrated, so for instance if there's only one then it can totally escape the +scheme. + + +These functions below acquire VGA resources for the given card and mark those +resources as locked. If the resources requested are "normal" (and not legacy) +resources, the arbiter will first check whether the card is doing legacy +decoding for that type of resource. If yes, the lock is "converted" into a +legacy resource lock. The arbiter will first look for all VGA cards that +might conflict and disable their IOs and/or Memory access, including VGA +forwarding on P2P bridges if necessary, so that the requested resources can +be used. Then, the card is marked as locking these resources and the IO and/or +Memory access is enabled on the card (including VGA forwarding on parent +P2P bridges if any). In the case of vga_arb_lock(), the function will block +if some conflicting card is already locking one of the required resources (or +any resource on a different bus segment, since P2P bridges don't differentiate +VGA memory and IO afaik). If the card already owns the resources, the function +succeeds. vga_arb_trylock() will return (-EBUSY) instead of blocking. Nested +calls are supported (a per-resource counter is maintained). + + +Set the target device of this client. + int pci_device_vgaarb_set_target (struct pci_device *dev); + + +For instance, in x86 if two devices on the same bus want to lock different +resources, both will succeed (lock). If devices are in different buses and +trying to lock different resources, only the first who tried succeeds. + int pci_device_vgaarb_lock (void); + int pci_device_vgaarb_trylock (void); + +Unlock resources of device. + int pci_device_vgaarb_unlock (void); + +Indicates to the arbiter if the card decodes legacy VGA IOs, legacy VGA +Memory, both, or none. All cards default to both, the card driver (fbdev for +example) should tell the arbiter if it has disabled legacy decoding, so the +card can be left out of the arbitration process (and can be safe to take +interrupts at any time. + int pci_device_vgaarb_decodes (int new_vgaarb_rsrc); + +Connects to the arbiter device, allocates the struct + int pci_device_vgaarb_init (void); + +Close the connection + void pci_device_vgaarb_fini (void); + + +I.3 xf86VGAArbiter (X server implementation) +-------------------------------------------- + +(TODO) + +X server basically wraps all the functions that touch VGA registers somehow. + + +II. Credits +=========== + +Benjamin Herrenschmidt (IBM?) started this work when he discussed such design +with the Xorg community in 2005 [1, 2]. In the end of 2007, Paulo Zanoni and +Tiago Vignatti (both of C3SL/Federal University of Paraná) proceeded his work +enhancing the kernel code to adapt as a kernel module and also did the +implementation of the user space side [3]. Now (2009) Tiago Vignatti and Dave +Airlie finally put this work in shape and queued to Jesse Barnes' PCI tree. + + +III. References +============== + +[0] http://cgit.freedesktop.org/xorg/xserver/commit/?id=4b42448a2388d40f257774fbffdccaea87bd0347 +[1] http://lists.freedesktop.org/archives/xorg/2005-March/006663.html +[2] http://lists.freedesktop.org/archives/xorg/2005-March/006745.html +[3] http://lists.freedesktop.org/archives/xorg/2007-October/029507.html
Signature · 1 line
1.5.6.3
Hi Jesse,
Just for completeness, there are some perfectionisms clean ups that I catch bellow.
Quoted 11 lines · Dave Airlie
From: Tiago Vignatti <[email protected]>
Background:
Graphic devices are accessed through ranges in I/O or memory space. While most modern devices allow relocation of such ranges, some "Legacy" VGA devices implemented on PCI will typically have the same "hard-decoded" addresses as they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware Revision 2.1" Section 7, Legacy Devices.The Resource Access Control (RAC) module inside the X server currently does
RAC already gone away. Finally!
Quoted 5 lines
the task of arbitration when more than one legacy device co-exists on the same machine. But the problem happens when these devices are trying to be accessed by different userspace clients (e.g. two server in parallel). Their address assignments conflict. Therefore an arbitration scheme _outside_ of the X server is needed to control the sharing of these resources. This document
^^^^^^^^
AFAIK a patch is not a document :)
Quoted 1,348 lines
introduces the operation of the VGA arbiter implemented for Linux kernel.
Signed-off-by Tiago Vignatti <[email protected]>Signed-off-by Dave Airlie <[email protected]>--- drivers/gpu/Makefile | 2 +- drivers/gpu/vga/Kconfig | 10 + drivers/gpu/vga/Makefile | 1 + drivers/gpu/vga/vgaarb.c | 1206 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 44 ++ drivers/video/Kconfig | 2 + include/linux/pci.h | 2 + include/linux/vgaarb.h | 195 ++++++++ 8 files changed, 1461 insertions(+), 1 deletions(-) create mode 100644 drivers/gpu/vga/Kconfig create mode 100644 drivers/gpu/vga/Makefile create mode 100644 drivers/gpu/vga/vgaarb.c create mode 100644 include/linux/vgaarb.hdrivers/gpu/Makefile +1
-1@@ -1 +1 @@ -obj-y += drm/ +obj-y += drm/ vga/drivers/gpu/vga/Kconfig new file +10
-0@@ -0,0 +1,10 @@ +config VGA_ARB + bool "VGA Arbitration" if EMBEDDED + default y + depends on PCI + help + Some "legacy" VGA devices implemented on PCI typically have the same + hard-decoded addresses as they did on ISA. When multiple PCI devices + are accessed at same time they need some kind of coordination. Please + see Documentation/vgaarbiter.txt for more details. Select this to + enable VGA arbiter.drivers/gpu/vga/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-$(CONFIG_VGA_ARB) += vgaarb.odrivers/gpu/vga/vgaarb.c new file +1206
-0@@ -0,0 +1,1206 @@ +/* +* vgaarb.c +* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]> +* (C) Copyright 2007 Paulo R. Zanoni <[email protected]> +* (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]> +* +* Implements the VGA arbitration. For details refer to +* Documentation/vgaarbiter.txt +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/pci.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/wait.h> +#include <linux/spinlock.h> +#include <linux/poll.h> +#include <linux/miscdevice.h> + +#include <linux/uaccess.h> + +#include <linux/vgaarb.h> + +static void vga_arbiter_notify_clients (void ); +/* +* We keep a list of all vga devices in the system to speed +* up the various operations of the arbiter +*/ +struct vga_device { +struct list_head list; +struct pci_dev *pdev; +unsigned int decodes;/* what does it decodes */ +unsigned int owns;/* what does it owns */ +unsigned int locks;/* what does it locks */ +unsigned int io_lock_cnt;/* legacy IO lock count */ +unsigned int mem_lock_cnt;/* legacy MEM lock count */ +unsigned int io_norm_cnt;/* normal IO count */ +unsigned int mem_norm_cnt;/* normal MEM count */ + +/* allow IRQ enable/disable hook */ +void *cookie; +void (*irq_set_state)(void *cookie,bool enable); +unsigned int (*set_vga_decode)(void *cookie,bool decode); +}; + +static LIST_HEAD (vga_list); +static int vga_count, vga_decode_count; +static bool vga_arbiter_used; +static DEFINE_SPINLOCK (vga_lock); +static DECLARE_WAIT_QUEUE_HEAD (vga_wait_queue); + + +static const char *vga_iostate_to_str (unsigned int iostate) +{ +/* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ + iostate &=VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +switch (iostate) { +case VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM : +return "io+mem" ; +case VGA_RSRC_LEGACY_IO : +return "io" ; +case VGA_RSRC_LEGACY_MEM : +return "mem" ; + } +return "none" ; +} + +static int vga_str_to_iostate (char *buf,int str_size,int *io_state) +{ +/* we could in theory hand out locks on IO and mem +* separately to userspace but it can cause deadlocks */ +if (strncmp (buf,"none" ,4 ) ==0 ) { + *io_state =VGA_RSRC_NONE ; +return 1 ; + } + +/* XXX We're not chekcing the str_size! */ +if (strncmp (buf,"io+mem" ,6 ) ==0 ) + goto both; +else if (strncmp (buf,"io" ,2 ) ==0 ) + goto both; +else if (strncmp (buf,"mem" ,3 ) ==0 ) + goto both; +return 0 ; +both: + *io_state =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +return 1 ; +} + +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +/* this is only used a cookie - it should not be dereferenced */ +static struct pci_dev *vga_default; +#endif + +static void vga_arb_device_card_gone (struct pci_dev *pdev); + +/* Find somebody in our list */ +static struct vga_device *vgadev_find (struct pci_dev *pdev) +{ +struct vga_device *vgadev; + +list_for_each_entry (vgadev, &vga_list, list) +if (pdev == vgadev->pdev) +return vgadev; +return NULL ; +} + +/* Returns the default VGA device (vgacon's babe) */ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +struct pci_dev *vga_default_device (void ) +{ +return vga_default; +} +#endif + +static inline void vga_irq_set_state (struct vga_device *vgadev,bool state) +{ +if (vgadev->irq_set_state) + vgadev->irq_set_state (vgadev->cookie, state); +} + + +/* If we don't ever use VGA arb we should avoid +turning off anything anywhere due to old X servers getting +confused about the boot device not being VGA */ +static void vga_check_first_use (void ) +{ +/* we should inform all GPUs in the system that +* VGA arb has occured and to try and disable resources +* if they can */ +if (!vga_arbiter_used) { + vga_arbiter_used = true; +vga_arbiter_notify_clients (); + } +} + +static struct vga_device *__vga_tryget (struct vga_device *vgadev, +unsigned int rsrc) +{ +unsigned int wants, legacy_wants, match; +struct vga_device *conflict; +unsigned int pci_bits; +/* Account for "normal" resources to lock. If we decode the legacy, +* counterpart, we need to request it as well +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && + (vgadev->decodes &VGA_RSRC_LEGACY_IO )) + rsrc |=VGA_RSRC_LEGACY_IO ; +if ((rsrc &VGA_RSRC_NORMAL_MEM ) && + (vgadev->decodes &VGA_RSRC_LEGACY_MEM )) + rsrc |=VGA_RSRC_LEGACY_MEM ; + +pr_devel ("%s: %d\n" , __func__, rsrc); +pr_devel ("%s: owns: %d\n" , __func__, vgadev->owns); + +/* Check what resources we need to acquire */ + wants = rsrc & ~vgadev->owns; + +/* We already own everything, just mark locked & bye bye */ +if (wants ==0 ) + goto lock_them; + +/* We don't need to request a legacy resource, we just enable +* appropriate decoding and go +*/ + legacy_wants = wants &VGA_RSRC_LEGACY_MASK ; +if (legacy_wants ==0 ) + goto enable_them; + +/* Ok, we don't, let's find out how we need to kick off */ +list_for_each_entry (conflict, &vga_list, list) { +unsigned int lwants = legacy_wants; +unsigned int change_bridge =0 ; + +/* Don't conflict with myself */ +if (vgadev == conflict) +continue ; + +/* Check if the architecture allows a conflict between those +* 2 devices or if they are on separate domains +*/ +if (!vga_conflicts (vgadev->pdev, conflict->pdev)) +continue ; + +/* We have a possible conflict. before we go further, we must +* check if we sit on the same bus as the conflicting device. +* if we don't, then we must tie both IO and MEM resources +* together since there is only a single bit controlling +* VGA forwarding on P2P bridges +*/ +if (vgadev->pdev->bus != conflict->pdev->bus) { + change_bridge =1 ; + lwants =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; + } + +/* Check if the guy has a lock on the resource. If he does, +* return the conflicting entry +*/ +if (conflict->locks & lwants) +return conflict; + +/* Ok, now check if he owns the resource we want. We don't need +* to check "decodes" since it should be impossible to own +* own legacy resources you don't decode unless I have a bug +* in this code... +*/ +WARN_ON (conflict->owns & ~conflict->decodes); + match = lwants & conflict->owns; +if (!match) +continue ; + +/* looks like he doesn't have a lock, we can steal +* them from him +*/ +vga_irq_set_state (conflict, false); + + pci_bits =0 ; +if (lwants & (VGA_RSRC_LEGACY_MEM |VGA_RSRC_NORMAL_MEM )) + pci_bits |=PCI_COMMAND_MEMORY ; +if (lwants & (VGA_RSRC_LEGACY_IO |VGA_RSRC_NORMAL_IO )) + pci_bits |=PCI_COMMAND_IO ; + +pci_set_vga_state (conflict->pdev, false, pci_bits, + change_bridge); + conflict->owns &= ~lwants; +/* If he also owned non-legacy, that is no longer the case */ +if (lwants &VGA_RSRC_LEGACY_MEM ) + conflict->owns &= ~VGA_RSRC_NORMAL_MEM ; +if (lwants &VGA_RSRC_LEGACY_IO ) + conflict->owns &= ~VGA_RSRC_NORMAL_IO ; + } + +enable_them: +/* ok dude, we got it, everybody conflicting has been disabled, let's +* enable us. Make sure we don't mark a bit in "owns" that we don't +* also have in "decodes". We can lock resources we don't decode but +* not own them. +*/ + pci_bits =0 ; +if (wants & (VGA_RSRC_LEGACY_MEM |VGA_RSRC_NORMAL_MEM )) + pci_bits |=PCI_COMMAND_MEMORY ; +if (wants & (VGA_RSRC_LEGACY_IO |VGA_RSRC_NORMAL_IO )) + pci_bits |=PCI_COMMAND_IO ; +pci_set_vga_state (vgadev->pdev, true, pci_bits, !!(wants &VGA_RSRC_LEGACY_MASK )); + +vga_irq_set_state (vgadev, true); + vgadev->owns |= (wants & vgadev->decodes); +lock_them: + vgadev->locks |= (rsrc &VGA_RSRC_LEGACY_MASK ); +if (rsrc &VGA_RSRC_LEGACY_IO ) + vgadev->io_lock_cnt++; +if (rsrc &VGA_RSRC_LEGACY_MEM ) + vgadev->mem_lock_cnt++; +if (rsrc &VGA_RSRC_NORMAL_IO ) + vgadev->io_norm_cnt++; +if (rsrc &VGA_RSRC_NORMAL_MEM ) + vgadev->mem_norm_cnt++; + +return NULL ; +} + +static void __vga_put (struct vga_device *vgadev,unsigned int rsrc) +{ +unsigned int old_locks = vgadev->locks; + +pr_devel ("%s\n" , __func__); + +/* Update our counters, and account for equivalent legacy resources +* if we decode them +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && vgadev->io_norm_cnt >0 ) { + vgadev->io_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_IO ) + rsrc |=VGA_RSRC_LEGACY_IO ; + } +if ((rsrc &VGA_RSRC_NORMAL_MEM ) && vgadev->mem_norm_cnt >0 ) { + vgadev->mem_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_MEM ) + rsrc |=VGA_RSRC_LEGACY_MEM ; + } +if ((rsrc &VGA_RSRC_LEGACY_IO ) && vgadev->io_lock_cnt >0 ) + vgadev->io_lock_cnt--; +if ((rsrc &VGA_RSRC_LEGACY_MEM ) && vgadev->mem_lock_cnt >0 ) + vgadev->mem_lock_cnt--; + +/* Just clear lock bits, we do lazy operations so we don't really +* have to bother about anything else at this point +*/ +if (vgadev->io_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_IO ; +if (vgadev->mem_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_MEM ; + +/* Kick the wait queue in case somebody was waiting if we actually +* released something +*/ +if (old_locks != vgadev->locks) +wake_up_all (&vga_wait_queue); +} + +int vga_get (struct pci_dev *pdev,unsigned int rsrc,int interruptible) +{ +struct vga_device *vgadev, *conflict; +unsigned long flags; +wait_queue_t wait; +int rc =0 ; + +vga_check_first_use (); +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; + +for (;;) { +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +spin_unlock_irqrestore (&vga_lock, flags); + rc = -ENODEV ; +break ; + } + conflict =__vga_tryget (vgadev, rsrc); +spin_unlock_irqrestore (&vga_lock, flags); +if (conflict ==NULL ) +break ; + + +/* We have a conflict, we wait until somebody kicks the +* work queue. Currently we have one work queue that we +* kick each time some resources are released, but it would +* be fairly easy to have a per device one so that we only +* need to attach to the conflicting device +*/ +init_waitqueue_entry (&wait, current); +add_wait_queue (&vga_wait_queue, &wait); +set_current_state (interruptible ? +TASK_INTERRUPTIBLE : +TASK_UNINTERRUPTIBLE ); +if (signal_pending (current)) { + rc = -EINTR ; +break ; + } +schedule (); +remove_wait_queue (&vga_wait_queue, &wait); +set_current_state (TASK_RUNNING ); + } +return rc; +} +EXPORT_SYMBOL (vga_get); + +int vga_tryget (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; +int rc =0 ; + +vga_check_first_use (); + +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + rc = -ENODEV ; + goto bail; + } +if (__vga_tryget (vgadev, rsrc)) + rc = -EBUSY ; +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return rc; +} +EXPORT_SYMBOL (vga_tryget); + +void vga_put (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; + +/* The one who calls us should check for this, but lets be sure... */ +if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; +__vga_put (vgadev, rsrc); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +} +EXPORT_SYMBOL (vga_put); + +/* +* Currently, we assume that the "initial" setup of the system is +* not sane, that is we come up with conflicting devices and let +* the arbiter's client decides if devices decodes or not legacy +* things. +*/ +static bool vga_arbiter_add_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + +/* Only deal with VGA class devices */ +if ((pdev->class >>8 ) !=PCI_CLASS_DISPLAY_VGA ) +return false; + +/* Allocate structure */ + vgadev =kmalloc (sizeof (struct vga_device ),GFP_KERNEL ); +if (vgadev ==NULL ) { +pr_err ("vgaarb: failed to allocate pci device\n" ); +/* What to do on allocation failure ? For now, let's +* just do nothing, I'm not sure there is anything saner +* to be done +*/ +return false; + } + +memset (vgadev,0 ,sizeof (*vgadev)); + +/* Take lock & check for duplicates */ +spin_lock_irqsave (&vga_lock, flags); +if (vgadev_find (pdev) !=NULL ) { +BUG_ON (1 ); + goto fail; + } + vgadev->pdev = pdev; + +/* By default, assume we decode everything */ + vgadev->decodes =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM | +VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; + +/* by default mark it as decoding */ + vga_decode_count++; +/* Mark that we "own" resources based on our enables, we will +* clear that below if the bridge isn't forwarding +*/ +pci_read_config_word (pdev,PCI_COMMAND , &cmd); +if (cmd &PCI_COMMAND_IO ) + vgadev->owns |=VGA_RSRC_LEGACY_IO ; +if (cmd &PCI_COMMAND_MEMORY ) + vgadev->owns |=VGA_RSRC_LEGACY_MEM ; + +/* Check if VGA cycles can get down to us */ + bus = pdev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { +u16 l; +pci_read_config_word (bridge,PCI_BRIDGE_CONTROL , + &l); +if (!(l &PCI_BRIDGE_CTL_VGA )) { + vgadev->owns =0 ; +break ; + } + } + bus = bus->parent; + } + +/* Deal with VGA default device. Use first enabled one +* by default if arch doesn't have it's own hook +*/ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +if (vga_default ==NULL && + ((vgadev->owns &VGA_RSRC_LEGACY_MASK ) ==VGA_RSRC_LEGACY_MASK )) + vga_default =pci_dev_get (pdev); +#endif + +/* Add to the list */ +list_add (&vgadev->list, &vga_list); + vga_count++; +pr_info ("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n" , +pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), +vga_iostate_to_str (vgadev->locks)); + +spin_unlock_irqrestore (&vga_lock, flags); +return true; +fail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return false; +} + +static bool vga_arbiter_del_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +bool ret = true; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + ret = false; + goto bail; + } + +if (vga_default == pdev) { +pci_dev_put (vga_default); + vga_default =NULL ; + } + +if (vgadev->decodes & (VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM )) + vga_decode_count--; + +/* Remove entry from list */ +list_del (&vgadev->list); + vga_count--; +/* Notify userland driver that the device is gone so it discards +* it's copies of the pci_dev pointer +*/ +vga_arb_device_card_gone (pdev); + +/* Wake up all possible waiters */ +wake_up_all (&vga_wait_queue); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return ret; +} + +/* this is called with the lock */ +static inline void vga_update_device_decodes (struct vga_device *vgadev, +int new_decodes) +{ +int old_decodes; +struct vga_device *new_vgadev, *conflict; + + old_decodes = vgadev->decodes; + vgadev->decodes = new_decodes; + +pr_info ("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n" , +pci_name (vgadev->pdev), +vga_iostate_to_str (old_decodes), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns)); + + +/* if we own the decodes we should move them along to +another card */ +if ((vgadev->owns & old_decodes) && (vga_count >1 )) { +/* set us to own nothing */ + vgadev->owns &= ~old_decodes; +list_for_each_entry (new_vgadev, &vga_list, list) { +if ((new_vgadev != vgadev) && + (new_vgadev->decodes &VGA_RSRC_LEGACY_MASK )) { +pr_info ("vgaarb: transferring owner from PCI:%s to PCI:%s\n" ,pci_name (vgadev->pdev),pci_name (new_vgadev->pdev)); + conflict =__vga_tryget (new_vgadev,VGA_RSRC_LEGACY_MASK ); +if (!conflict) +__vga_put (new_vgadev,VGA_RSRC_LEGACY_MASK ); +break ; + } + } + } + +/* change decodes counter */ +if (old_decodes != new_decodes) { +if (new_decodes & (VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM )) + vga_decode_count++; +else + vga_decode_count--; + } +} + +void __vga_set_legacy_decoding (struct pci_dev *pdev,unsigned int decodes,bool userspace) +{ +struct vga_device *vgadev; +unsigned long flags; + + decodes &=VGA_RSRC_LEGACY_MASK ; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; + +/* don't let userspace futz with kernel driver decodes */ +if (userspace && vgadev->set_vga_decode) + goto bail; + +/* update the device decodes + counter */ +vga_update_device_decodes (vgadev, decodes); + +/* XXX if somebody is going from "doesn't decode" to "decodes" state +* here, additional care must be taken as we may have pending owner +* ship of non-legacy region ... +*/ +bail: +spin_unlock_irqrestore (&vga_lock, flags); +} + +void vga_set_legacy_decoding (struct pci_dev *pdev,unsigned int decodes) +{ +__vga_set_legacy_decoding (pdev, decodes, false); +} +EXPORT_SYMBOL (vga_set_legacy_decoding); + +/* return number of active VGA devices */ +/* call with NULL to unregister */ +int vga_client_register (struct pci_dev *pdev,void *cookie, +void (*irq_set_state)(void *cookie,bool state), +unsigned int (*set_vga_decode)(void *cookie,bool decode)) +{ +int ret =-1 ; +struct vga_device *vgadev; +unsigned long flags; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (!vgadev) + goto bail; + + vgadev->irq_set_state = irq_set_state; + vgadev->set_vga_decode = set_vga_decode; + vgadev->cookie = cookie; + ret =0 ; + +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return ret; + +} +EXPORT_SYMBOL (vga_client_register); + +/* +* Char driver implementation +* +* Semantics is: +* +* open : open user instance of the arbitrer. by default, it's +* attached to the default VGA device of the system. +* +* close : close user instance, release locks +* +* read : return a string indicating the status of the target. +* an IO state string is of the form {io,mem,io+mem,none}, +* mc and ic are respectively mem and io lock counts (for +* debugging/diagnostic only). "decodes" indicate what the +* card currently decodes, "owns" indicates what is currently +* enabled on it, and "locks" indicates what is locked by this +* card. If the card is unplugged, we get "invalid" then for +* card_ID and an -ENODEV error is returned for any command +* until a new card is targeted +* +* "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)" +* +* write : write a command to the arbiter. List of commands is: +* +* target <card_ID> : switch target to card <card_ID> (see below) +* lock <io_state> : acquires locks on target ("none" is invalid io_state) +* trylock <io_state> : non-blocking acquire locks on target +* unlock <io_state> : release locks on target +* unlock all : release all locks on target held by this user +* decodes <io_state> : set the legacy decoding attributes for the card +* +* poll : event if something change on any card (not just the target) +* +* card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default" +* to go back to the system default card (TODO: not implemented yet). +* Currently, only PCI is supported as a prefix, but the userland API may +* support other bus types in the future, even if the current kernel +* implementation doesn't. +* +* Note about locks: +* +* The driver keeps track of which user has what locks on which card. It +* supports stacking, like the kernel one. This complexifies the implementation +* a bit, but makes the arbiter more tolerant to userspace problems and able +* to properly cleanup in all cases when a process dies. +* Currently, a max of 16 cards simultaneously can have locks issued from +* userspace for a given user (file descriptor instance) of the arbiter. +* +* If the device is hot-unplugged, there is a hook inside the module to notify +* they being added/removed in the system and automatically added/removed in +* the arbiter. +*/ + +#define MAX_USER_CARDS 16 +#define PCI_INVALID_CARD ((struct pci_dev *)-1UL) + +/* +* Each user has an array of these, tracking which cards have locks +*/ +struct vga_arb_user_card { +struct pci_dev *pdev; +unsigned int mem_cnt; +unsigned int io_cnt; +}; + +struct vga_arb_private { +struct list_head list; +struct pci_dev *target; +struct vga_arb_user_card cards[MAX_USER_CARDS ]; +spinlock_t lock; +}; + +static LIST_HEAD (vga_user_list); +static DEFINE_SPINLOCK (vga_user_lock); + + +/* +* This function gets a string in the format: "PCI:domain:bus:dev.fn" and +* returns the respective values. If the string is not in this format, +* it returns 0. +*/ +static int vga_pci_str_to_vars (char *buf,int count,unsigned int *domain, +unsigned int *bus,unsigned int *devfn) +{ +int n; +unsigned int slot, func; + + + n =sscanf (buf,"PCI:%x:%x:%x.%x" , domain, bus, &slot, &func); +if (n !=4 ) +return 0 ; + + *devfn =PCI_DEVFN (slot, func); + +return 1 ; +} + +static ssize_t vga_arb_read (struct file *file,char __user * buf, +size_t count,loff_t *ppos) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_device *vgadev; +struct pci_dev *pdev; +unsigned long flags; +size_t len; +int rc; +char *lbuf; + + lbuf =kmalloc (1024 ,GFP_KERNEL ); +if (lbuf ==NULL ) +return -ENOMEM ; + +/* Shields against vga_arb_device_card_gone (pci_dev going +* away), and allows access to vga list +*/ +spin_lock_irqsave (&vga_lock, flags); + +/* If we are targetting the default, use it */ + pdev = priv->target; +if (pdev ==NULL || pdev ==PCI_INVALID_CARD ) { +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Find card vgadev structure */ + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +/* Wow, it's not in the list, that shouldn't happen, +* let's fix us up and return invalid card +*/ +if (pdev == priv->target) +vga_arb_device_card_gone (pdev); +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Fill the buffer with infos */ + len =snprintf (lbuf,1024 , +"count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n" , + vga_decode_count,pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), +vga_iostate_to_str (vgadev->locks), + vgadev->io_lock_cnt, vgadev->mem_lock_cnt); + +spin_unlock_irqrestore (&vga_lock, flags); +done: + +/* Copy that to user */ +if (len > count) + len = count; + rc =copy_to_user (buf, lbuf, len); +kfree (lbuf); +if (rc) +return -EFAULT ; +return len; +} + +/* +* TODO: To avoid parsing inside kernel and to improve the speed we may +* consider use ioctl here +*/ +static ssize_t vga_arb_write (struct file *file,const char __user * buf, +size_t count,loff_t *ppos) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc =NULL ; +struct pci_dev *pdev; + +unsigned int io_state; + +char *kbuf, *curr_pos; +size_t remaining = count; + +int ret_val; +int i; + + + kbuf =kmalloc (count +1 ,GFP_KERNEL ); +if (!kbuf) +return -ENOMEM ; + +if (copy_from_user (kbuf, buf, count)) { +kfree (kbuf); +return -EFAULT ; + } + curr_pos = kbuf; + kbuf[count] ='\0' ;/* Just to make sure... */ + +if (strncmp (curr_pos,"lock " ,5 ) ==0 ) { + curr_pos +=5 ; + remaining -=5 ; + +pr_devel ("client 0x%X called 'lock'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +if (io_state ==VGA_RSRC_NONE ) { + ret_val = -EPROTO ; + goto done; + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +vga_get_uninterruptible (pdev, io_state); + +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { +if (io_state &VGA_RSRC_LEGACY_IO ) + priv->cards[i].io_cnt++; +if (io_state &VGA_RSRC_LEGACY_MEM ) + priv->cards[i].mem_cnt++; +break ; + } + } + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"unlock " ,7 ) ==0 ) { + curr_pos +=7 ; + remaining -=7 ; + +pr_devel ("client 0x%X called 'unlock'\n" , (int )priv); + +if (strncmp (curr_pos,"all" ,3 ) ==0 ) + io_state =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +else { +if (!vga_str_to_iostate + (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) + uc = &priv->cards[i]; + } + +if (!uc) +return -EINVAL ; + +if (io_state &VGA_RSRC_LEGACY_IO && uc->io_cnt ==0 ) +return -EINVAL ; + +if (io_state &VGA_RSRC_LEGACY_MEM && uc->mem_cnt ==0 ) +return -EINVAL ; + +vga_put (pdev, io_state); + +if (io_state &VGA_RSRC_LEGACY_IO ) + uc->io_cnt--; +if (io_state &VGA_RSRC_LEGACY_MEM ) + uc->mem_cnt--; + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"trylock " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; + +pr_devel ("client 0x%X called 'trylock'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +if (vga_tryget (pdev, io_state)) { +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { +if (io_state &VGA_RSRC_LEGACY_IO ) + priv->cards[i].io_cnt++; +if (io_state &VGA_RSRC_LEGACY_MEM ) + priv->cards[i].mem_cnt++; +break ; + } + } + ret_val = count; + goto done; + }else { + ret_val = -EBUSY ; + goto done; + } + + }else if (strncmp (curr_pos,"target " ,7 ) ==0 ) { +unsigned int domain, bus, devfn; +struct vga_device *vgadev; + + curr_pos +=7 ; + remaining -=7 ; +pr_devel ("client 0x%X called 'target'\n" , (int )priv); +/* if target is default */ +if (!strncmp (buf,"default" ,7 )) + pdev =pci_dev_get (vga_default_device ()); +else { +if (!vga_pci_str_to_vars (curr_pos, remaining, + &domain, &bus, &devfn)) { + ret_val = -EPROTO ; + goto done; + } + + pdev =pci_get_bus_and_slot (bus, devfn); +if (!pdev) { +pr_info ("vgaarb: invalid PCI address!\n" ); + ret_val = -ENODEV ; + goto done; + } + } + + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +pr_info ("vgaarb: this pci device is not a vga device\n" ); +pci_dev_put (pdev); + ret_val = -ENODEV ; + goto done; + } + + priv->target = pdev; +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) +break ; +if (priv->cards[i].pdev ==NULL ) { + priv->cards[i].pdev = pdev; + priv->cards[i].io_cnt =0 ; + priv->cards[i].mem_cnt =0 ; +break ; + } + } +if (i ==MAX_USER_CARDS ) { +pr_err ("vgaarb: maximum user cards number reached!\n" ); +pci_dev_put (pdev); +/* XXX: which value to return? */ + ret_val = -ENOMEM ; + goto done; + } + + ret_val = count; +pci_dev_put (pdev); + goto done; + + + }else if (strncmp (curr_pos,"decodes " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; +pr_devel ("vgaarb: client 0x%X called 'decodes'\n" , (int )priv); + +if (!vga_str_to_iostate (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +__vga_set_legacy_decoding (pdev, io_state, true); + ret_val = count; + goto done; + } +/* If we got here, the message written is not part of the protocol! */ +kfree (kbuf); +return -EPROTO ; + +done: +kfree (kbuf); +return ret_val; +} + +static unsigned int vga_arb_fpoll (struct file *file,poll_table * wait) +{ +struct vga_arb_private *priv = file->private_data; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; +poll_wait (file, &vga_wait_queue, wait); +return POLLIN ; +} + +static int vga_arb_open (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv; +unsigned long flags; + +pr_devel ("%s\n" , __func__); + + priv =kmalloc (sizeof (struct vga_arb_private ),GFP_KERNEL ); +if (priv ==NULL ) +return -ENOMEM ; +memset (priv,0 ,sizeof (*priv)); +spin_lock_init (&priv->lock); + file->private_data = priv; + +spin_lock_irqsave (&vga_user_lock, flags); +list_add (&priv->list, &vga_user_list); +spin_unlock_irqrestore (&vga_user_lock, flags); + +/* Set the client' lists of locks */ + priv->target =vga_default_device ();/* Maybe this is still null! */ + priv->cards[0 ].pdev = priv->target; + priv->cards[0 ].io_cnt =0 ; + priv->cards[0 ].mem_cnt =0 ; + + +return 0 ; +} + +static int vga_arb_release (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc; +unsigned long flags; +int i; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; + +spin_lock_irqsave (&vga_user_lock, flags); +list_del (&priv->list); +for (i =0 ; i <MAX_USER_CARDS ; i++) { + uc = &priv->cards[i]; +if (uc->pdev ==NULL ) +continue ; +pr_devel ("uc->io_cnt == %d, uc->mem_cnt == %d\n" , + uc->io_cnt, uc->mem_cnt); +while (uc->io_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_IO ); +while (uc->mem_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_MEM ); + } +spin_unlock_irqrestore (&vga_user_lock, flags); + +kfree (priv); + +return 0 ; +} + +static void vga_arb_device_card_gone (struct pci_dev *pdev) +{ +} + +/* +* callback any registered clients to let them know we have a +* change in VGA cards +*/ +static void vga_arbiter_notify_clients (void ) +{ +struct vga_device *vgadev; +unsigned long flags; +uint32_t new_decodes; +bool new_state; + +if (!vga_arbiter_used) +return ; + +spin_lock_irqsave (&vga_lock, flags); +list_for_each_entry (vgadev, &vga_list, list) { +if (vga_count >1 ) + new_state = false; +else + new_state = true; +if (vgadev->set_vga_decode) { + new_decodes = vgadev->set_vga_decode (vgadev->cookie, new_state); +vga_update_device_decodes (vgadev, new_decodes); + } + } +spin_unlock_irqrestore (&vga_lock, flags); +} + +static int pci_notify (struct notifier_block *nb,unsigned long action, +void *data) +{ +struct device *dev = data; +struct pci_dev *pdev =to_pci_dev (dev); +bool notify = false; + +pr_devel ("%s\n" , __func__); + +/* For now we're only intereted in devices added and removed. I didn't +* test this thing here, so someone needs to double check for the +* cases of hotplugable vga cards. */ +if (action ==BUS_NOTIFY_ADD_DEVICE ) + notify =vga_arbiter_add_pci_device (pdev); +else if (action ==BUS_NOTIFY_DEL_DEVICE ) + notify =vga_arbiter_del_pci_device (pdev); + +if (notify) +vga_arbiter_notify_clients (); +return 0 ; +} + +static struct notifier_block pci_notifier = { + .notifier_call = pci_notify, +}; + +static const struct file_operations vga_arb_device_fops = { + .read = vga_arb_read, + .write = vga_arb_write, + .poll = vga_arb_fpoll, + .open = vga_arb_open, + .release = vga_arb_release, +}; + +static struct miscdevice vga_arb_device = { +MISC_DYNAMIC_MINOR ,"vga_arbiter" , &vga_arb_device_fops +}; + +static int __init vga_arb_device_init (void ) +{ +int rc; +struct pci_dev *pdev; + + rc =misc_register (&vga_arb_device); +if (rc <0 ) +pr_err ("vgaarb: error %d registering device\n" , rc); + +bus_register_notifier (&pci_bus_type, &pci_notifier); + +/* We add all pci devices satisfying vga class in the arbiter by +* default */ + pdev =NULL ; +while ((pdev = +pci_get_subsys (PCI_ANY_ID ,PCI_ANY_ID ,PCI_ANY_ID , +PCI_ANY_ID , pdev)) !=NULL ) +vga_arbiter_add_pci_device (pdev); + +pr_info ("vgaarb: loaded\n" ); +return rc; +} +subsys_initcall (vga_arb_device_init);drivers/pci/pci.c +44
-0@@ -2502,6 +2502,50 @@ int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)return 0 ; } +/** +* pci_set_vga_state - set VGA decode state on device and parents if requested +* @dev the PCI device +* @decode - true = enable decoding, false = disable decoding +* @command_bits PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY +* @change_bridge - traverse ancestors and change bridges +*/ +int pci_set_vga_state (struct pci_dev *dev,bool decode, +unsigned int command_bits,bool change_bridge) +{ +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + +WARN_ON (command_bits & ~(PCI_COMMAND_IO |PCI_COMMAND_MEMORY )); + +pci_read_config_word (dev,PCI_COMMAND , &cmd); +if (decode == true) + cmd |= command_bits; +else + cmd &= ~command_bits; +pci_write_config_word (dev,PCI_COMMAND , cmd); + +if (change_bridge == false) +return 0 ; + + bus = dev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { +pci_read_config_word (bridge,PCI_BRIDGE_CONTROL , + &cmd); +if (decode == true) + cmd |=PCI_BRIDGE_CTL_VGA ; +else + cmd &= ~PCI_BRIDGE_CTL_VGA ; +pci_write_config_word (bridge,PCI_BRIDGE_CONTROL , + cmd); + } + bus = bus->parent; + } +return 0 ; +} +#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZEstatic char resource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE ] = {0 };spinlock_t resource_alignment_lock =SPIN_LOCK_UNLOCKED ;drivers/video/Kconfig +2
-0@@ -7,6 +7,8 @@ menu "Graphics support" source "drivers/char/agp/Kconfig" +source "drivers/gpu/vga/Kconfig" + source "drivers/gpu/drm/Kconfig" config VGASTATEinclude/linux/pci.h +2
-0@@ -805,6 +805,8 @@ int pci_cfg_space_size_ext(struct pci_dev *dev);int pci_cfg_space_size (struct pci_dev *dev);unsigned char pci_bus_max_busnr (struct pci_bus *bus); +int pci_set_vga_state (struct pci_dev *pdev,bool decode, +unsigned int command_bits,bool change_bridge);/* kmem_cache style wrapper around pci_alloc_consistent() */ #include <linux/dmapool.h> include/linux/vgaarb.h new file +0
-0
include/linux/vgaarb.h line 2
Quoted 2 lines
include/linux/vgaarb.h new file +2
-0@@ -0,0 +1,195 @@ +/* +* vgaarb.c
^^^^^^^^
change to vgaarb.h
Quoted 194 lines
include/linux/vgaarb.h new file +193
-1+* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]> +* (C) Copyright 2007 Paulo R. Zanoni <[email protected]> +* (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]> +*/ + +#ifndef LINUX_VGA_H + +#include <asm/vga.h> + +/* Legacy VGA regions */ +#define VGA_RSRC_NONE 0x00 +#define VGA_RSRC_LEGACY_IO 0x01 +#define VGA_RSRC_LEGACY_MEM 0x02 +#define VGA_RSRC_LEGACY_MASK (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM) +/* Non-legacy access */ +#define VGA_RSRC_NORMAL_IO 0x04 +#define VGA_RSRC_NORMAL_MEM 0x08 + +/* Passing that instead of a pci_dev to use the system "default" +* device, that is the one used by vgacon. Archs will probably +* have to provide their own vga_default_device(); +*/ +#define VGA_DEFAULT_DEVICE (NULL) + +/* For use by clients */ + +/** +* vga_set_legacy_decoding +* +* @pdev: pci device of the VGA card +* @decodes: bit mask of what legacy regions the card decodes +* +* Indicates to the arbiter if the card decodes legacy VGA IOs, +* legacy VGA Memory, both, or none. All cards default to both, +* the card driver (fbdev for example) should tell the arbiter +* if it has disabled legacy decoding, so the card can be left +* out of the arbitration process (and can be safe to take +* interrupts at any time. +*/ +extern void vga_set_legacy_decoding (struct pci_dev *pdev, +unsigned int decodes); + +/** +* vga_get - acquire & locks VGA resources +* +* pdev: pci device of the VGA card or NULL for the system default +* rsrc: bit mask of resources to acquire and lock +* interruptible: blocking should be interruptible by signals ? +* +* This function acquires VGA resources for the given +* card and mark those resources locked. If the resource requested +* are "normal" (and not legacy) resources, the arbiter will first check +* wether the card is doing legacy decoding for that type of resource. If +* yes, the lock is "converted" into a legacy resource lock. +* The arbiter will first look for all VGA cards that might conflict +* and disable their IOs and/or Memory access, inlcuding VGA forwarding +* on P2P bridges if necessary, so that the requested resources can +* be used. Then, the card is marked as locking these resources and +* the IO and/or Memory accesse are enabled on the card (including +* VGA forwarding on parent P2P bridges if any). +* This function will block if some conflicting card is already locking +* one of the required resources (or any resource on a different bus +* segment, since P2P bridges don't differenciate VGA memory and IO +* afaik). You can indicate wether this blocking should be interruptible +* by a signal (for userland interface) or not. +* Must not be called at interrupt time or in atomic context. +* If the card already owns the resources, the function succeeds. +* Nested calls are supported (a per-resource counter is maintained) +*/ + +extern int vga_get (struct pci_dev *pdev,unsigned int rsrc, +int interruptible); + +/** +* vga_get_interruptible +* +* Shortcut to vga_get +*/ + +static inline int vga_get_interruptible (struct pci_dev *pdev, +unsigned int rsrc) +{ +return vga_get (pdev, rsrc,1 ); +} + +/** +* vga_get_interruptible +* +* Shortcut to vga_get +*/ + +static inline int vga_get_uninterruptible (struct pci_dev *pdev, +unsigned int rsrc) +{ +return vga_get (pdev, rsrc,0 ); +} + +/** +* vga_tryget - try to acquire & lock legacy VGA resources +* +* @pdev: pci devivce of VGA card or NULL for system default +* @rsrc: bit mask of resources to acquire and lock +* +* This function performs the same operation as vga_get(), but +* will return an error (-EBUSY) instead of blocking if the resources +* are already locked by another card. It can be called in any context +*/ + +extern int vga_tryget (struct pci_dev *pdev,unsigned int rsrc); + +/** +* vga_put - release lock on legacy VGA resources +* +* @pdev: pci device of VGA card or NULL for system default +* @rsrc: but mask of resource to release +* +* This function releases resources previously locked by vga_get() +* or vga_tryget(). The resources aren't disabled right away, so +* that a subsequence vga_get() on the same card will succeed +* immediately. Resources have a counter, so locks are only +* released if the counter reaches 0. +*/ + +extern void vga_put (struct pci_dev *pdev,unsigned int rsrc); + + +/** +* vga_default_device +* +* This can be defined by the platform. The default implementation +* is rather dumb and will probably only work properly on single +* vga card setups and/or x86 platforms. +* +* If your VGA default device is not PCI, you'll have to return +* NULL here. In this case, I assume it will not conflict with +* any PCI card. If this is not true, I'll have to define two archs +* hooks for enabling/disabling the VGA default device if that is +* possible. This may be a problem with real _ISA_ VGA cards, in +* addition to a PCI one. I don't know at this point how to deal +* with that card. Can theirs IOs be disabled at all ? If not, then +* I suppose it's a matter of having the proper arch hook telling +* us about it, so we basically never allow anybody to succeed a +* vga_get()... +*/ + +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +extern struct pci_dev *vga_default_device (void ); +#endif + +/** +* vga_conflicts +* +* Architectures should define this if they have several +* independant PCI domains that can afford concurrent VGA +* decoding +*/ + +#ifndef __ARCH_HAS_VGA_CONFLICT +static inline int vga_conflicts (struct pci_dev *p1,struct pci_dev *p2) +{ +return 1 ; +} +#endif + +/* +* Register a client with the VGA arbitration logic +* return value: number of VGA devices in system. +* +* Clients have two callback mechanisms they can use. +* irq enable/disable callback - +* If a client can't disable its GPUs VGA resources, then we +* need to be able to ask it to turn off its irqs when we +* turn off its mem and io decoding. +* set_vga_decode +* If a client can disable its GPU VGA resource, it will +* get a callback from this to set the encode/decode state +* +* Clients with disable abilities should check the return value +* of this function and if the VGA device count is > 1, should +* disable VGA decoding resources. +* +* Rationale: we cannot disable VGA decode resources unconditionally +* some single GPU laptops seem to require ACPI or BIOS access to the +* VGA registers to control things like backlights etc. +* Hopefully newer multi-GPU laptops do something saner, and desktops +* won't have any special ACPI for this. +*/ +int vga_client_register (struct pci_dev *pdev,void *cookie, +void (*irq_set_state)(void *cookie,bool state), +unsigned int (*set_vga_decode)(void *cookie,bool state)); + +#endif /* LINUX_VGA_H */ --
Quoted 1 line
1.6.0.6
Tiago
Can you send a patch for this stuff? Maybe it could be rolled in with a fix for the warnings...
Thanks,
Jesse
Quoted 1,750 lines · On Sun, 16 Aug 2009 18:17:46 +0300 Tiago Vignatti <[email protected]>…
Hi Jesse,
Just for completeness, there are some perfectionisms clean ups that I catch bellow.
Quoted 11 lines · Dave Airlie
From: Tiago Vignatti <[email protected]>
Background:
Graphic devices are accessed through ranges in I/O or memory space. While most modern devices allow relocation of such ranges, some "Legacy" VGA devices implemented on PCI will typically have the same "hard-decoded" addresses as they did on ISA. For more details see "PCI Bus Binding to IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware Revision 2.1" Section 7, Legacy Devices.Quoted 2 lines
The Resource Access Control (RAC) module inside the X server currently does
RAC already gone away. Finally!
Quoted 6 lines
the task of arbitration when more than one legacy device co-exists on the same machine. But the problem happens when these devices are trying to be accessed by different userspace clients (e.g. two server in parallel). Their address assignments conflict. Therefore an arbitration scheme _outside_ of the X server is needed to control the sharing of these resources. This document
^^^^^^^^
AFAIK a patch is not a
document :)Quoted 1,467 lines
introduces the operation of the VGA arbiter implemented for Linux kernel.
Signed-off-by Tiago Vignatti <[email protected]>Signed-off-by Dave Airlie <[email protected]>--- drivers/gpu/Makefile | 2 +- drivers/gpu/vga/Kconfig | 10 + drivers/gpu/vga/Makefile | 1 + drivers/gpu/vga/vgaarb.c | 1206 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 44 ++ drivers/video/Kconfig | 2 + include/linux/pci.h | 2 + include/linux/vgaarb.h | 195 ++++++++ 8 files changed, 1461 insertions(+), 1 deletions(-) create mode 100644 drivers/gpu/vga/Kconfig create mode 100644 drivers/gpu/vga/Makefile create mode 100644 drivers/gpu/vga/vgaarb.c create mode 100644 include/linux/vgaarb.hdrivers/gpu/Makefile +1
-1@@ -1 +1 @@ -obj-y += drm/ +obj-y += drm/ vga/drivers/gpu/vga/Kconfig new file +6
-0@@ -0,0 +1,10 @@ +config VGA_ARB + bool "VGA Arbitration" if EMBEDDED + default y + depends on PCI + help + Some "legacy" VGA devices implemented on PCI typicallyhave the same
+ hard-decoded addresses as they did on ISA. When multiple
PCI devices
+ are accessed at same time they need some kind of
coordination. Please
+ seeDocumentation/vgaarbiter.txtfor more details. Select
this todrivers/gpu/vga/Kconfig new file +1
-0+ enable VGA arbiter.drivers/gpu/vga/Makefile new file +1
-0@@ -0,0 +1 @@ +obj-$(CONFIG_VGA_ARB) += vgaarb.odrivers/gpu/vga/vgaarb.c new file +4
-0@@ -0,0 +1,1206 @@ +/* +* vgaarb.c +* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]>
+ * (C) Copyright 2007 Paulo R. Zanoni <[email protected]>
+ * (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]>
+ *
+ * Implements the VGA arbitration. For details refer to
+ *Documentation/vgaarbiter.txt
+ */drivers/gpu/vga/vgaarb.c new file +62
-0+ +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/pci.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/wait.h> +#include <linux/spinlock.h> +#include <linux/poll.h> +#include <linux/miscdevice.h> + +#include <linux/uaccess.h> + +#include <linux/vgaarb.h> + +static void vga_arbiter_notify_clients (void ); +/* +* We keep a list of all vga devices in the system to speed +* up the various operations of the arbiter +*/ +struct vga_device { +struct list_head list; +struct pci_dev *pdev; +unsigned int decodes;/* what does it decodes */ +unsigned int owns;/* what does it owns */ +unsigned int locks;/* what does it locks */ +unsigned int io_lock_cnt;/* legacy IO lock count */ +unsigned int mem_lock_cnt;/* legacy MEM lock count */ +unsigned int io_norm_cnt;/* normal IO count */ +unsigned int mem_norm_cnt;/* normal MEM count */ + +/* allow IRQ enable/disable hook */ +void *cookie; +void (*irq_set_state)(void *cookie,bool enable); +unsigned int (*set_vga_decode)(void *cookie,bool decode); +}; + +static LIST_HEAD (vga_list); +static int vga_count, vga_decode_count; +static bool vga_arbiter_used; +static DEFINE_SPINLOCK (vga_lock); +static DECLARE_WAIT_QUEUE_HEAD (vga_wait_queue); + + +static const char *vga_iostate_to_str (unsigned int iostate) +{ +/* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */ + iostate &=VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +switch (iostate) { +case VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM : +return "io+mem" ; +case VGA_RSRC_LEGACY_IO : +return "io" ; +case VGA_RSRC_LEGACY_MEM : +return "mem" ; + } +return "none" ; +} + +static int vga_str_to_iostate(char *buf, int str_size, int
*io_state) +{drivers/gpu/vga/vgaarb.c new file +47
-0+/* we could in theory hand out locks on IO and mem +* separately to userspace but it can cause deadlocks */ +if (strncmp (buf,"none" ,4 ) ==0 ) { + *io_state =VGA_RSRC_NONE ; +return 1 ; + } + +/* XXX We're not chekcing the str_size! */ +if (strncmp (buf,"io+mem" ,6 ) ==0 ) + goto both; +else if (strncmp (buf,"io" ,2 ) ==0 ) + goto both; +else if (strncmp (buf,"mem" ,3 ) ==0 ) + goto both; +return 0 ; +both: + *io_state =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM ; +return 1 ; +} + +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +/* this is only used a cookie - it should not be dereferenced */ +static struct pci_dev *vga_default; +#endif + +static void vga_arb_device_card_gone (struct pci_dev *pdev); + +/* Find somebody in our list */ +static struct vga_device *vgadev_find (struct pci_dev *pdev) +{ +struct vga_device *vgadev; + +list_for_each_entry (vgadev, &vga_list, list) +if (pdev == vgadev->pdev) +return vgadev; +return NULL ; +} + +/* Returns the default VGA device (vgacon's babe) */ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +struct pci_dev *vga_default_device (void ) +{ +return vga_default; +} +#endif + +static inline void vga_irq_set_state(struct vga_device *vgadev,bool state) +{
drivers/gpu/vga/vgaarb.c new file +26
-0+if (vgadev->irq_set_state) + vgadev->irq_set_state (vgadev->cookie, state); +} + + +/* If we don't ever use VGA arb we should avoid +turning off anything anywhere due to old X servers getting +confused about the boot device not being VGA */ +static void vga_check_first_use (void ) +{ +/* we should inform all GPUs in the system that +* VGA arb has occured and to try and disable resources +* if they can */ +if (!vga_arbiter_used) { + vga_arbiter_used = true; +vga_arbiter_notify_clients (); + } +} + +static struct vga_device *__vga_tryget (struct vga_device *vgadev, +unsigned int rsrc) +{ +unsigned int wants, legacy_wants, match; +struct vga_device *conflict; +unsigned int pci_bits; +/* Account for "normal" resources to lock. If we decode the legacy,
drivers/gpu/vga/vgaarb.c new file +20
-0+* counterpart, we need to request it as well +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && + (vgadev->decodes &VGA_RSRC_LEGACY_IO )) + rsrc |=VGA_RSRC_LEGACY_IO ; +if ((rsrc &VGA_RSRC_NORMAL_MEM ) && + (vgadev->decodes &VGA_RSRC_LEGACY_MEM )) + rsrc |=VGA_RSRC_LEGACY_MEM ; + +pr_devel ("%s: %d\n" , __func__, rsrc); +pr_devel ("%s: owns: %d\n" , __func__, vgadev->owns); + +/* Check what resources we need to acquire */ + wants = rsrc & ~vgadev->owns; + +/* We already own everything, just mark locked & bye bye */ +if (wants ==0 ) + goto lock_them; + +/* We don't need to request a legacy resource, we just enable
drivers/gpu/vga/vgaarb.c new file +16
-0+* appropriate decoding and go +*/ + legacy_wants = wants &VGA_RSRC_LEGACY_MASK ; +if (legacy_wants ==0 ) + goto enable_them; + +/* Ok, we don't, let's find out how we need to kick off */ +list_for_each_entry (conflict, &vga_list, list) { +unsigned int lwants = legacy_wants; +unsigned int change_bridge =0 ; + +/* Don't conflict with myself */ +if (vgadev == conflict) +continue ; + +/* Check if the architecture allows a conflict between those
drivers/gpu/vga/vgaarb.c new file +6
-0+* 2 devices or if they are on separate domains +*/ +if (!vga_conflicts (vgadev->pdev, conflict->pdev)) +continue ; + +/* We have a possible conflict. before we go further, we must
+ * check if we sit on the same bus as the
conflicting device.
+ * if we don't, then we must tie both IO and MEM
resources
+ * together since there is only a single bit
controllingdrivers/gpu/vga/vgaarb.c new file +5
-0+* VGA forwarding on P2P bridges +*/ +if (vgadev->pdev->bus != conflict->pdev->bus) { + change_bridge =1 ; + lwants = VGA_RSRC_LEGACY_IO |
VGA_RSRC_LEGACY_MEM;drivers/gpu/vga/vgaarb.c new file +3
-0+ } + +/* Check if the guy has a lock on the resource. If he does,
drivers/gpu/vga/vgaarb.c new file +6
-0+* return the conflicting entry +*/ +if (conflict->locks & lwants) +return conflict; + +/* Ok, now check if he owns the resource we want. We don't need
+ * to check "decodes" since it should be impossible
to own
+ * own legacy resources you don't decode unless I
have a bugdrivers/gpu/vga/vgaarb.c new file +14
-0+* in this code... +*/ +WARN_ON (conflict->owns & ~conflict->decodes); + match = lwants & conflict->owns; +if (!match) +continue ; + +/* looks like he doesn't have a lock, we can steal +* them from him +*/ +vga_irq_set_state (conflict, false); + + pci_bits =0 ; + if (lwants &(VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
drivers/gpu/vga/vgaarb.c new file +2
-0+ pci_bits |=PCI_COMMAND_MEMORY ; + if (lwants &(VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
drivers/gpu/vga/vgaarb.c new file +6
-0+ pci_bits |=PCI_COMMAND_IO ; + +pci_set_vga_state (conflict->pdev, false, pci_bits, + change_bridge); + conflict->owns &= ~lwants; +/* If he also owned non-legacy, that is no longer the case */
drivers/gpu/vga/vgaarb.c new file +8
-0+if (lwants &VGA_RSRC_LEGACY_MEM ) + conflict->owns &= ~VGA_RSRC_NORMAL_MEM ; +if (lwants &VGA_RSRC_LEGACY_IO ) + conflict->owns &= ~VGA_RSRC_NORMAL_IO ; + } + +enable_them: +/* ok dude, we got it, everybody conflicting has been disabled, let's
+ * enable us. Make sure we don't mark a bit in "owns" that
we don't
+ * also have in "decodes". We can lock resources we don't
decode butdrivers/gpu/vga/vgaarb.c new file +8
-0+* not own them. +*/ + pci_bits =0 ; +if (wants & (VGA_RSRC_LEGACY_MEM |VGA_RSRC_NORMAL_MEM )) + pci_bits |=PCI_COMMAND_MEMORY ; +if (wants & (VGA_RSRC_LEGACY_IO |VGA_RSRC_NORMAL_IO )) + pci_bits |=PCI_COMMAND_IO ; + pci_set_vga_state(vgadev->pdev, true, pci_bits, !!(wants &
VGA_RSRC_LEGACY_MASK)); +drivers/gpu/vga/vgaarb.c new file +23
-0+vga_irq_set_state (vgadev, true); + vgadev->owns |= (wants & vgadev->decodes); +lock_them: + vgadev->locks |= (rsrc &VGA_RSRC_LEGACY_MASK ); +if (rsrc &VGA_RSRC_LEGACY_IO ) + vgadev->io_lock_cnt++; +if (rsrc &VGA_RSRC_LEGACY_MEM ) + vgadev->mem_lock_cnt++; +if (rsrc &VGA_RSRC_NORMAL_IO ) + vgadev->io_norm_cnt++; +if (rsrc &VGA_RSRC_NORMAL_MEM ) + vgadev->mem_norm_cnt++; + +return NULL ; +} + +static void __vga_put (struct vga_device *vgadev,unsigned int rsrc) +{ +unsigned int old_locks = vgadev->locks; + +pr_devel ("%s\n" , __func__); + +/* Update our counters, and account for equivalent legacy resources
drivers/gpu/vga/vgaarb.c new file +3
-0+* if we decode them +*/ +if ((rsrc &VGA_RSRC_NORMAL_IO ) && vgadev->io_norm_cnt >0 ){
drivers/gpu/vga/vgaarb.c new file +5
-0+ vgadev->io_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_IO ) + rsrc |=VGA_RSRC_LEGACY_IO ; + } + if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt >0) {
drivers/gpu/vga/vgaarb.c new file +7
-0+ vgadev->mem_norm_cnt--; +if (vgadev->decodes &VGA_RSRC_LEGACY_MEM ) + rsrc |=VGA_RSRC_LEGACY_MEM ; + } +if ((rsrc &VGA_RSRC_LEGACY_IO ) && vgadev->io_lock_cnt >0 ) + vgadev->io_lock_cnt--; + if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt >0)
drivers/gpu/vga/vgaarb.c new file +3
-0+ vgadev->mem_lock_cnt--; + +/* Just clear lock bits, we do lazy operations so we don't really
drivers/gpu/vga/vgaarb.c new file +8
-0+* have to bother about anything else at this point +*/ +if (vgadev->io_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_IO ; +if (vgadev->mem_lock_cnt ==0 ) + vgadev->locks &= ~VGA_RSRC_LEGACY_MEM ; + +/* Kick the wait queue in case somebody was waiting if we actually
drivers/gpu/vga/vgaarb.c new file +7
-0+* released something +*/ +if (old_locks != vgadev->locks) +wake_up_all (&vga_wait_queue); +} + +int vga_get(struct pci_dev *pdev, unsigned int rsrc, intinterruptible) +{
drivers/gpu/vga/vgaarb.c new file +7
-0+struct vga_device *vgadev, *conflict; +unsigned long flags; +wait_queue_t wait; +int rc =0 ; + +vga_check_first_use (); +/* The one who calls us should check for this, but lets be sure... */
drivers/gpu/vga/vgaarb.c new file +20
-0+if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; + +for (;;) { +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +spin_unlock_irqrestore (&vga_lock, flags); + rc = -ENODEV ; +break ; + } + conflict =__vga_tryget (vgadev, rsrc); +spin_unlock_irqrestore (&vga_lock, flags); +if (conflict ==NULL ) +break ; + + +/* We have a conflict, we wait until somebody kicks the
+ * work queue. Currently we have one work queue
that we
+ * kick each time some resources are released, but
it would
+ * be fairly easy to have a per device one so that
we onlydrivers/gpu/vga/vgaarb.c new file +28
-0+* need to attach to the conflicting device +*/ +init_waitqueue_entry (&wait, current); +add_wait_queue (&vga_wait_queue, &wait); +set_current_state (interruptible ? +TASK_INTERRUPTIBLE : +TASK_UNINTERRUPTIBLE ); +if (signal_pending (current)) { + rc = -EINTR ; +break ; + } +schedule (); +remove_wait_queue (&vga_wait_queue, &wait); +set_current_state (TASK_RUNNING ); + } +return rc; +} +EXPORT_SYMBOL (vga_get); + +int vga_tryget (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; +int rc =0 ; + +vga_check_first_use (); + +/* The one who calls us should check for this, but lets be sure... */
drivers/gpu/vga/vgaarb.c new file +24
-0+if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return 0 ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + rc = -ENODEV ; + goto bail; + } +if (__vga_tryget (vgadev, rsrc)) + rc = -EBUSY ; +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return rc; +} +EXPORT_SYMBOL (vga_tryget); + +void vga_put (struct pci_dev *pdev,unsigned int rsrc) +{ +struct vga_device *vgadev; +unsigned long flags; + +/* The one who calls us should check for this, but lets be sure... */
drivers/gpu/vga/vgaarb.c new file +38
-0+if (pdev ==NULL ) + pdev =vga_default_device (); +if (pdev ==NULL ) +return ; +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; +__vga_put (vgadev, rsrc); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +} +EXPORT_SYMBOL (vga_put); + +/* +* Currently, we assume that the "initial" setup of the system is +* not sane, that is we come up with conflicting devices and let +* the arbiter's client decides if devices decodes or not legacy +* things. +*/ +static bool vga_arbiter_add_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + +/* Only deal with VGA class devices */ +if ((pdev->class >>8 ) !=PCI_CLASS_DISPLAY_VGA ) +return false; + +/* Allocate structure */ + vgadev =kmalloc (sizeof (struct vga_device ),GFP_KERNEL ); +if (vgadev ==NULL ) { +pr_err ("vgaarb: failed to allocate pci device\n" ); +/* What to do on allocation failure ? For now, let's +* just do nothing, I'm not sure there is anything saner
drivers/gpu/vga/vgaarb.c new file +22
-0+* to be done +*/ +return false; + } + +memset (vgadev,0 ,sizeof (*vgadev)); + +/* Take lock & check for duplicates */ +spin_lock_irqsave (&vga_lock, flags); +if (vgadev_find (pdev) !=NULL ) { +BUG_ON (1 ); + goto fail; + } + vgadev->pdev = pdev; + +/* By default, assume we decode everything */ + vgadev->decodes =VGA_RSRC_LEGACY_IO |VGA_RSRC_LEGACY_MEM | +VGA_RSRC_NORMAL_IO |VGA_RSRC_NORMAL_MEM ; + +/* by default mark it as decoding */ + vga_decode_count++; +/* Mark that we "own" resources based on our enables, we will
drivers/gpu/vga/vgaarb.c new file +15
-0+* clear that below if the bridge isn't forwarding +*/ +pci_read_config_word (pdev,PCI_COMMAND , &cmd); +if (cmd &PCI_COMMAND_IO ) + vgadev->owns |=VGA_RSRC_LEGACY_IO ; +if (cmd &PCI_COMMAND_MEMORY ) + vgadev->owns |=VGA_RSRC_LEGACY_MEM ; + +/* Check if VGA cycles can get down to us */ + bus = pdev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { +u16 l; + pci_read_config_word(bridge,
PCI_BRIDGE_CONTROL,drivers/gpu/vga/vgaarb.c new file +15
-0+ &l); +if (!(l &PCI_BRIDGE_CTL_VGA )) { + vgadev->owns =0 ; +break ; + } + } + bus = bus->parent; + } + +/* Deal with VGA default device. Use first enabled one +* by default if arch doesn't have it's own hook +*/ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE + if (vga_default == NULL && + ((vgadev->owns & VGA_RSRC_LEGACY_MASK) ==
VGA_RSRC_LEGACY_MASK))drivers/gpu/vga/vgaarb.c new file +7
-0+ vga_default =pci_dev_get (pdev); +#endif + +/* Add to the list */ +list_add (&vgadev->list, &vga_list); + vga_count++; + pr_info("vgaarb: device added:PCI:%s,decodes=%s,owns=%s,locks=%s\n",
drivers/gpu/vga/vgaarb.c new file +32
-0+pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), + vga_iostate_to_str(vgadev->locks)); + +spin_unlock_irqrestore (&vga_lock, flags); +return true; +fail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return false; +} + +static bool vga_arbiter_del_pci_device (struct pci_dev *pdev) +{ +struct vga_device *vgadev; +unsigned long flags; +bool ret = true; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + ret = false; + goto bail; + } + +if (vga_default == pdev) { +pci_dev_put (vga_default); + vga_default =NULL ; + } + + if (vgadev->decodes & (VGA_RSRC_LEGACY_IO |
VGA_RSRC_LEGACY_MEM))drivers/gpu/vga/vgaarb.c new file +6
-0+ vga_decode_count--; + +/* Remove entry from list */ +list_del (&vgadev->list); + vga_count--; +/* Notify userland driver that the device is gone so it discards
drivers/gpu/vga/vgaarb.c new file +14
-0+* it's copies of the pci_dev pointer +*/ +vga_arb_device_card_gone (pdev); + +/* Wake up all possible waiters */ +wake_up_all (&vga_wait_queue); +bail: +spin_unlock_irqrestore (&vga_lock, flags); +kfree (vgadev); +return ret; +} + +/* this is called with the lock */ +static inline void vga_update_device_decodes(struct vga_device*vgadev,
drivers/gpu/vga/vgaarb.c new file +9
-0+ int new_decodes) +{ +int old_decodes; +struct vga_device *new_vgadev, *conflict; + + old_decodes = vgadev->decodes; + vgadev->decodes = new_decodes; + + pr_info("vgaarb: device changed decodes:PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
drivers/gpu/vga/vgaarb.c new file +14
-0+pci_name (vgadev->pdev), +vga_iostate_to_str (old_decodes), +vga_iostate_to_str (vgadev->decodes), + vga_iostate_to_str(vgadev->owns)); + + +/* if we own the decodes we should move them along to +another card */ +if ((vgadev->owns & old_decodes) && (vga_count >1 )) { +/* set us to own nothing */ + vgadev->owns &= ~old_decodes; +list_for_each_entry (new_vgadev, &vga_list, list) { + if ((new_vgadev != vgadev) && + (new_vgadev->decodes &
VGA_RSRC_LEGACY_MASK)) {
+ pr_info("vgaarb: transferring owner
from PCI:%s to PCI:%s\n",pci_name(vgadev->pdev),pci_name(new_vgadev->pdev));
+ conflict = __vga_tryget(new_vgadev,VGA_RSRC_LEGACY_MASK);drivers/gpu/vga/vgaarb.c new file +2
-0+if (!conflict) + __vga_put(new_vgadev,
VGA_RSRC_LEGACY_MASK);drivers/gpu/vga/vgaarb.c new file +8
-0+break ; + } + } + } + +/* change decodes counter */ +if (old_decodes != new_decodes) { + if (new_decodes & (VGA_RSRC_LEGACY_IO |
VGA_RSRC_LEGACY_MEM))drivers/gpu/vga/vgaarb.c new file +7
-0+ vga_decode_count++; + else + vga_decode_count--; + } +} + +void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned intdecodes, bool userspace) +{
drivers/gpu/vga/vgaarb.c new file +18
-0+struct vga_device *vgadev; +unsigned long flags; + + decodes &=VGA_RSRC_LEGACY_MASK ; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) + goto bail; + +/* don't let userspace futz with kernel driver decodes */ +if (userspace && vgadev->set_vga_decode) + goto bail; + +/* update the device decodes + counter */ +vga_update_device_decodes (vgadev, decodes); + +/* XXX if somebody is going from "doesn't decode" to "decodes" state
+ * here, additional care must be taken as we may have
pending ownerdrivers/gpu/vga/vgaarb.c new file +7
-0+* ship of non-legacy region ... +*/ +bail: +spin_unlock_irqrestore (&vga_lock, flags); +} + +void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned intdecodes) +{
drivers/gpu/vga/vgaarb.c new file +8
-0+__vga_set_legacy_decoding (pdev, decodes, false); +} +EXPORT_SYMBOL (vga_set_legacy_decoding); + +/* return number of active VGA devices */ +/* call with NULL to unregister */ +int vga_client_register(struct pci_dev *pdev, void *cookie, + void (*irq_set_state)(void *cookie, boolstate),
+ unsigned int (*set_vga_decode)(void
*cookie, bool decode)) +{drivers/gpu/vga/vgaarb.c new file +27
-0+int ret =-1 ; +struct vga_device *vgadev; +unsigned long flags; + +spin_lock_irqsave (&vga_lock, flags); + vgadev =vgadev_find (pdev); +if (!vgadev) + goto bail; + + vgadev->irq_set_state = irq_set_state; + vgadev->set_vga_decode = set_vga_decode; + vgadev->cookie = cookie; + ret =0 ; + +bail: +spin_unlock_irqrestore (&vga_lock, flags); +return ret; + +} +EXPORT_SYMBOL (vga_client_register); + +/* +* Char driver implementation +* +* Semantics is: +* +* open : open user instance of the arbitrer. by default, it's
+ * attached to the default VGA device of the system.
+ *
+ * close : close user instance, release locks
+ *
+ * read : return a string indicating the status of the
target.
+ * an IO state string is of the form
{io,mem,io+mem,none},
+ * mc and ic are respectively mem and io lock
counts (for
+ * debugging/diagnostic only). "decodes" indicate
what the
+ * card currently decodes, "owns" indicates what is
currently
+ * enabled on it, and "locks" indicates what is
locked by this
+ * card. If the card is unplugged, we get "invalid"
then for
+ *card_IDand an-ENODEVerror is returned for any
command
+ * until a new card is targeted
+ *
+ * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
+ *
+ * write : write a command to the arbiter. List of commands
is:
+ *
+ * target <card_ID> : switch target to card <card_ID> (see
below)
+ * lock <io_state> : acquires locks on target ("none" is
invalidio_state)+ * trylock <io_state> : non-blocking acquire locks on target + * unlock <io_state> : release locks on target + * unlock all : release all locks on target held by thisuser
+ * decodes <io_state> : set the legacy decoding attributes for
the card
+ *
+ * poll : event if something change on any card (not just
the target)
+ *
+ *card_IDis of the form "PCI:domain:bus:dev.fn". It can be set to "default"
+ * to go back to the system default card (TODO: not implemented yet).
+ * Currently, only PCI is supported as a prefix, but the userland API may
+ * support other bus types in the future, even if the current kernel
+ * implementation doesn't.
+ *
+ * Note about locks:
+ *
+ * The driver keeps track of which user has what locks on which card. It
+ * supports stacking, like the kernel one. This complexifies the implementation
+ * a bit, but makes the arbiter more tolerant to userspace problems and able
+ * to properly cleanup in all cases when a process dies.
+ * Currently, a max of 16 cards simultaneously can have locks issued from
+ * userspace for a given user (file descriptor instance) of the arbiter.
+ *
+ * If the device is hot-unplugged, there is a hook inside the module to notify
+ * they being added/removed in the system and automatically added/removed in
+ * the arbiter.
+ */drivers/gpu/vga/vgaarb.c new file +26
-0+ +#define MAX_USER_CARDS 16 +#define PCI_INVALID_CARD ((struct pci_dev *)-1UL) + +/* +* Each user has an array of these, tracking which cards have locks +*/ +struct vga_arb_user_card { +struct pci_dev *pdev; +unsigned int mem_cnt; +unsigned int io_cnt; +}; + +struct vga_arb_private { +struct list_head list; +struct pci_dev *target; +struct vga_arb_user_card cards[MAX_USER_CARDS ]; +spinlock_t lock; +}; + +static LIST_HEAD (vga_user_list); +static DEFINE_SPINLOCK (vga_user_lock); + + +/* +* This function gets a string in the format: "PCI:domain:bus:dev.fn" and
+ * returns the respective values. If the string is not in this format,
+ * it returns 0.
+ */
+static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
+ unsigned int *bus, unsigned int
*devfn) +{drivers/gpu/vga/vgaarb.c new file +5
-0+int n; +unsigned int slot, func; + + + n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot,&func);
drivers/gpu/vga/vgaarb.c new file +52
-0+if (n !=4 ) +return 0 ; + + *devfn =PCI_DEVFN (slot, func); + +return 1 ; +} + +static ssize_t vga_arb_read (struct file *file,char __user * buf, +size_t count,loff_t *ppos) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_device *vgadev; +struct pci_dev *pdev; +unsigned long flags; +size_t len; +int rc; +char *lbuf; + + lbuf =kmalloc (1024 ,GFP_KERNEL ); +if (lbuf ==NULL ) +return -ENOMEM ; + +/* Shields against vga_arb_device_card_gone (pci_dev going +* away), and allows access to vga list +*/ +spin_lock_irqsave (&vga_lock, flags); + +/* If we are targetting the default, use it */ + pdev = priv->target; +if (pdev ==NULL || pdev ==PCI_INVALID_CARD ) { +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Find card vgadev structure */ + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { +/* Wow, it's not in the list, that shouldn't happen, +* let's fix us up and return invalid card +*/ +if (pdev == priv->target) +vga_arb_device_card_gone (pdev); +spin_unlock_irqrestore (&vga_lock, flags); + len =sprintf (lbuf,"invalid" ); + goto done; + } + +/* Fill the buffer with infos */ + len = snprintf(lbuf, 1024, +"count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
drivers/gpu/vga/vgaarb.c new file +21
-0+ vga_decode_count,pci_name (pdev), +vga_iostate_to_str (vgadev->decodes), +vga_iostate_to_str (vgadev->owns), +vga_iostate_to_str (vgadev->locks), + vgadev->io_lock_cnt, vgadev->mem_lock_cnt); + +spin_unlock_irqrestore (&vga_lock, flags); +done: + +/* Copy that to user */ +if (len > count) + len = count; + rc =copy_to_user (buf, lbuf, len); +kfree (lbuf); +if (rc) +return -EFAULT ; +return len; +} + +/* +* TODO: To avoid parsing inside kernel and to improve the speed we may
+ * consider use ioctl here
+ */
+staticssize_tvga_arb_write(struct file *file, const char__user
* buf,drivers/gpu/vga/vgaarb.c new file +33
-0+ size_t count, loff_t *ppos) +{ + struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc =NULL ; +struct pci_dev *pdev; + +unsigned int io_state; + +char *kbuf, *curr_pos; +size_t remaining = count; + +int ret_val; +int i; + + + kbuf =kmalloc (count +1 ,GFP_KERNEL ); +if (!kbuf) +return -ENOMEM ; + +if (copy_from_user (kbuf, buf, count)) { +kfree (kbuf); +return -EFAULT ; + } + curr_pos = kbuf; + kbuf[count] ='\0' ;/* Just to make sure... */ + + if (strncmp(curr_pos, "lock ", 5) == 0) { + curr_pos += 5; + remaining -= 5; + + pr_devel("client 0x%X called 'lock'\n", (int)priv); + + if (!vga_str_to_iostate(curr_pos, remaining,
&io_state)) {drivers/gpu/vga/vgaarb.c new file +34
-0+ ret_val = -EPROTO ; + goto done; + } +if (io_state ==VGA_RSRC_NONE ) { + ret_val = -EPROTO ; + goto done; + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +vga_get_uninterruptible (pdev, io_state); + +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { +if (io_state &VGA_RSRC_LEGACY_IO ) + priv->cards[i].io_cnt++; +if (io_state &VGA_RSRC_LEGACY_MEM ) + priv->cards[i].mem_cnt++; +break ; + } + } + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"unlock " ,7 ) ==0 ) { + curr_pos +=7 ; + remaining -=7 ; + + pr_devel("client 0x%X called 'unlock'\n",(int)priv); +
drivers/gpu/vga/vgaarb.c new file +2
-0+if (strncmp (curr_pos,"all" ,3 ) ==0 ) + io_state = VGA_RSRC_LEGACY_IO |
VGA_RSRC_LEGACY_MEM;drivers/gpu/vga/vgaarb.c new file +28
-0+ else { +if (!vga_str_to_iostate + (curr_pos, remaining, &io_state)) { + ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + } + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) + uc = &priv->cards[i]; + } + +if (!uc) +return -EINVAL ; + + if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt ==0)
drivers/gpu/vga/vgaarb.c new file +3
-0+return -EINVAL ; + + if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt== 0)
drivers/gpu/vga/vgaarb.c new file +16
-0+return -EINVAL ; + +vga_put (pdev, io_state); + +if (io_state &VGA_RSRC_LEGACY_IO ) + uc->io_cnt--; +if (io_state &VGA_RSRC_LEGACY_MEM ) + uc->mem_cnt--; + + ret_val = count; + goto done; + }else if (strncmp (curr_pos,"trylock " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; + + pr_devel("client 0x%X called 'trylock'\n",(int)priv); +
+ if (!vga_str_to_iostate(curr_pos, remaining,&io_state)) {drivers/gpu/vga/vgaarb.c new file +21
-0+ ret_val = -EPROTO ; + goto done; + } +/* TODO: Add this? +if (io_state == VGA_RSRC_NONE) { +ret_val = -EPROTO; +goto done; +} +*/ + + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +if (vga_tryget (pdev, io_state)) { +/* Update the client's locks lists... */ +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) { + if (io_state &
VGA_RSRC_LEGACY_IO)
+
priv->cards[i].io_cnt++;
+ if (io_state&VGA_RSRC_LEGACY_MEM)
+
priv->cards[i].mem_cnt++;drivers/gpu/vga/vgaarb.c new file +17
-0+break ; + } + } + ret_val = count; + goto done; + }else { + ret_val = -EBUSY ; + goto done; + } + + }else if (strncmp (curr_pos,"target " ,7 ) ==0 ) { +unsigned int domain, bus, devfn; +struct vga_device *vgadev; + + curr_pos +=7 ; + remaining -=7 ; + pr_devel("client 0x%X called 'target'\n",(int)priv);
drivers/gpu/vga/vgaarb.c new file +5
-0+/* if target is default */ +if (!strncmp (buf,"default" ,7 )) + pdev =pci_dev_get (vga_default_device ()); + else { + if (!vga_pci_str_to_vars(curr_pos,remaining,
+ &domain, &bus,
&devfn)) {drivers/gpu/vga/vgaarb.c new file +7
-0+ ret_val = -EPROTO ; + goto done; + } + + pdev =pci_get_bus_and_slot (bus, devfn); +if (!pdev) { + pr_info("vgaarb: invalid PCIaddress!\n");
drivers/gpu/vga/vgaarb.c new file +8
-0+ ret_val = -ENODEV ; + goto done; + } + } + + vgadev =vgadev_find (pdev); +if (vgadev ==NULL ) { + pr_info("vgaarb: this pci device is not avga device\n");
drivers/gpu/vga/vgaarb.c new file +18
-0+pci_dev_put (pdev); + ret_val = -ENODEV ; + goto done; + } + + priv->target = pdev; +for (i =0 ; i <MAX_USER_CARDS ; i++) { +if (priv->cards[i].pdev == pdev) +break ; +if (priv->cards[i].pdev ==NULL ) { + priv->cards[i].pdev = pdev; + priv->cards[i].io_cnt =0 ; + priv->cards[i].mem_cnt =0 ; +break ; + } + } +if (i ==MAX_USER_CARDS ) { + pr_err("vgaarb: maximum user cards numberreached!\n");
drivers/gpu/vga/vgaarb.c new file +15
-0+pci_dev_put (pdev); +/* XXX: which value to return? */ + ret_val = -ENOMEM ; + goto done; + } + + ret_val = count; +pci_dev_put (pdev); + goto done; + + + }else if (strncmp (curr_pos,"decodes " ,8 ) ==0 ) { + curr_pos +=8 ; + remaining -=8 ; + pr_devel("vgaarb: client 0x%X called 'decodes'\n",(int)priv); +
+ if (!vga_str_to_iostate(curr_pos, remaining,&io_state)) {drivers/gpu/vga/vgaarb.c new file +14
-0+ ret_val = -EPROTO ; + goto done; + } + pdev = priv->target; +if (priv->target ==NULL ) { + ret_val = -ENODEV ; + goto done; + } + +__vga_set_legacy_decoding (pdev, io_state, true); + ret_val = count; + goto done; + } +/* If we got here, the message written is not part of the protocol! */
drivers/gpu/vga/vgaarb.c new file +9
-0+kfree (kbuf); +return -EPROTO ; + +done: +kfree (kbuf); +return ret_val; +} + +static unsigned int vga_arb_fpoll(struct file *file, poll_table *wait) +{
drivers/gpu/vga/vgaarb.c new file +30
-0+struct vga_arb_private *priv = file->private_data; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; +poll_wait (file, &vga_wait_queue, wait); +return POLLIN ; +} + +static int vga_arb_open (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv; +unsigned long flags; + +pr_devel ("%s\n" , __func__); + + priv =kmalloc (sizeof (struct vga_arb_private ),GFP_KERNEL ); +if (priv ==NULL ) +return -ENOMEM ; +memset (priv,0 ,sizeof (*priv)); +spin_lock_init (&priv->lock); + file->private_data = priv; + +spin_lock_irqsave (&vga_user_lock, flags); +list_add (&priv->list, &vga_user_list); +spin_unlock_irqrestore (&vga_user_lock, flags); + +/* Set the client' lists of locks */ + priv->target =vga_default_device ();/* Maybe this is still null! */
drivers/gpu/vga/vgaarb.c new file +66
-0+ priv->cards[0 ].pdev = priv->target; + priv->cards[0 ].io_cnt =0 ; + priv->cards[0 ].mem_cnt =0 ; + + +return 0 ; +} + +static int vga_arb_release (struct inode *inode,struct file *file) +{ +struct vga_arb_private *priv = file->private_data; +struct vga_arb_user_card *uc; +unsigned long flags; +int i; + +pr_devel ("%s\n" , __func__); + +if (priv ==NULL ) +return -ENODEV ; + +spin_lock_irqsave (&vga_user_lock, flags); +list_del (&priv->list); +for (i =0 ; i <MAX_USER_CARDS ; i++) { + uc = &priv->cards[i]; +if (uc->pdev ==NULL ) +continue ; +pr_devel ("uc->io_cnt == %d, uc->mem_cnt == %d\n" , + uc->io_cnt, uc->mem_cnt); +while (uc->io_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_IO ); +while (uc->mem_cnt--) +vga_put (uc->pdev,VGA_RSRC_LEGACY_MEM ); + } +spin_unlock_irqrestore (&vga_user_lock, flags); + +kfree (priv); + +return 0 ; +} + +static void vga_arb_device_card_gone (struct pci_dev *pdev) +{ +} + +/* +* callback any registered clients to let them know we have a +* change in VGA cards +*/ +static void vga_arbiter_notify_clients (void ) +{ +struct vga_device *vgadev; +unsigned long flags; +uint32_t new_decodes; +bool new_state; + +if (!vga_arbiter_used) +return ; + +spin_lock_irqsave (&vga_lock, flags); +list_for_each_entry (vgadev, &vga_list, list) { +if (vga_count >1 ) + new_state = false; +else + new_state = true; +if (vgadev->set_vga_decode) { + new_decodes =
vgadev->set_vga_decode(vgadev->cookie, new_state);
+ vga_update_device_decodes(vgadev,new_decodes);drivers/gpu/vga/vgaarb.c new file +6
-0+ } + } +spin_unlock_irqrestore (&vga_lock, flags); +} + +static int pci_notify(struct notifier_block *nb, unsigned longaction,
drivers/gpu/vga/vgaarb.c new file +9
-0+ void *data) +{ +struct device *dev = data; +struct pci_dev *pdev =to_pci_dev (dev); +bool notify = false; + +pr_devel ("%s\n" , __func__); + +/* For now we're only intereted in devices added and removed. I didn't
+ * test this thing here, so someone needs to double check
for thedrivers/gpu/vga/vgaarb.c new file +39
-0+* cases of hotplugable vga cards. */ +if (action ==BUS_NOTIFY_ADD_DEVICE ) + notify =vga_arbiter_add_pci_device (pdev); +else if (action ==BUS_NOTIFY_DEL_DEVICE ) + notify =vga_arbiter_del_pci_device (pdev); + +if (notify) +vga_arbiter_notify_clients (); +return 0 ; +} + +static struct notifier_block pci_notifier = { + .notifier_call = pci_notify, +}; + +static const struct file_operations vga_arb_device_fops = { + .read = vga_arb_read, + .write = vga_arb_write, + .poll = vga_arb_fpoll, + .open = vga_arb_open, + .release = vga_arb_release, +}; + +static struct miscdevice vga_arb_device = { +MISC_DYNAMIC_MINOR ,"vga_arbiter" , &vga_arb_device_fops +}; + +static int __init vga_arb_device_init (void ) +{ +int rc; +struct pci_dev *pdev; + + rc =misc_register (&vga_arb_device); +if (rc <0 ) +pr_err ("vgaarb: error %d registering device\n" , rc); + +bus_register_notifier (&pci_bus_type, &pci_notifier); + +/* We add all pci devices satisfying vga class in the arbiter by
drivers/gpu/vga/vgaarb.c new file +11
-0+* default */ + pdev =NULL ; +while ((pdev = +pci_get_subsys (PCI_ANY_ID ,PCI_ANY_ID ,PCI_ANY_ID , +PCI_ANY_ID , pdev)) !=NULL ) +vga_arbiter_add_pci_device (pdev); + +pr_info ("vgaarb: loaded\n" ); +return rc; +} +subsys_initcall (vga_arb_device_init);drivers/pci/pci.c +2
-0@@ -2502,6 +2502,50 @@ int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type) return 0; } +/** +* pci_set_vga_state - set VGA decode state on device and parents if requested
+ * @dev the PCI device
+ * @decode - true = enable decoding, false = disable decoding
+ * @command_bitsPCI_COMMAND_IOand/orPCI_COMMAND_MEMORY
+ * @change_bridge - traverse ancestors and change bridges
+ */drivers/pci/pci.c +8
-0+int pci_set_vga_state (struct pci_dev *dev,bool decode, +unsigned int command_bits,bool change_bridge) +{ +struct pci_bus *bus; +struct pci_dev *bridge; +u16 cmd; + + WARN_ON(command_bits &~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY)); +
drivers/pci/pci.c +15
-0+pci_read_config_word (dev,PCI_COMMAND , &cmd); +if (decode == true) + cmd |= command_bits; +else + cmd &= ~command_bits; +pci_write_config_word (dev,PCI_COMMAND , cmd); + +if (change_bridge == false) +return 0 ; + + bus = dev->bus; +while (bus) { + bridge = bus->self; +if (bridge) { + pci_read_config_word(bridge,
PCI_BRIDGE_CONTROL,drivers/pci/pci.c +6
-0+ &cmd); +if (decode == true) + cmd |=PCI_BRIDGE_CTL_VGA ; +else + cmd &= ~PCI_BRIDGE_CTL_VGA ; + pci_write_config_word(bridge,
PCI_BRIDGE_CONTROL,drivers/pci/pci.c +7
-0+ cmd); + } + bus = bus->parent; + } +return 0 ; +} +#define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE static charresource_alignment_param[RESOURCE_ALIGNMENT_PARAM_SIZE] = {0};
spinlock_tresource_alignment_lock=SPIN_LOCK_UNLOCKED; diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig index 3b54b39..a0d9ee1 100644 --- a/drivers/video/Kconfig
+++ b/drivers/video/Kconfigdrivers/pci/pci.c +2
-0@@ -7,6 +7,8 @@ menu "Graphics support" source"drivers/char/agp/Kconfig" +source"drivers/gpu/vga/Kconfig" + source"drivers/gpu/drm/Kconfig" configVGASTATE include/linux/pci.h +2
-0@@ -805,6 +805,8 @@ int pci_cfg_space_size_ext(struct pci_dev *dev);int pci_cfg_space_size (struct pci_dev *dev);unsigned char pci_bus_max_busnr (struct pci_bus *bus); +int pci_set_vga_state (struct pci_dev *pdev,bool decode, + unsigned int command_bits, bool
change_bridge); /*kmem_cachestyle wrapper aroundpci_alloc_consistent()*/#include <
linux/dmapool.h>include/linux/vgaarb.h new file +0
-0include/linux/vgaarb.h line 2
Quoted 2 lines
include/linux/vgaarb.h new file +2
-0@@ -0,0 +1,195 @@ +/* +* vgaarb.c ^^^^^^^^
change tovgaarb.hQuoted 223 lines
include/linux/vgaarb.h new file +2
-0+* +* (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]>
+ * (C) Copyright 2007 Paulo R. Zanoni <[email protected]>
+ * (C) Copyright 2007, 2009 Tiago Vignatti <[email protected]>
+ */include/linux/vgaarb.h new file +10
-0+ +#ifndef LINUX_VGA_H + +#include <asm/vga.h> + +/* Legacy VGA regions */ +#define VGA_RSRC_NONE 0x00 +#define VGA_RSRC_LEGACY_IO 0x01 +#define VGA_RSRC_LEGACY_MEM 0x02 +#define VGA_RSRC_LEGACY_MASK (VGA_RSRC_LEGACY_IO |
VGA_RSRC_LEGACY_MEM) +/* Non-legacy access */include/linux/vgaarb.h new file +26
-0+#define VGA_RSRC_NORMAL_IO 0x04 +#define VGA_RSRC_NORMAL_MEM 0x08 + +/* Passing that instead of a pci_dev to use the system "default" +* device, that is the one used by vgacon. Archs will probably +* have to provide their own vga_default_device(); +*/ +#define VGA_DEFAULT_DEVICE (NULL) + +/* For use by clients */ + +/** +* vga_set_legacy_decoding +* +* @pdev: pci device of the VGA card +* @decodes: bit mask of what legacy regions the card decodes +* +* Indicates to the arbiter if the card decodes legacy VGA IOs, +* legacy VGA Memory, both, or none. All cards default to both, +* the card driver (fbdev for example) should tell the arbiter +* if it has disabled legacy decoding, so the card can be left +* out of the arbitration process (and can be safe to take +* interrupts at any time. +*/ +extern void vga_set_legacy_decoding(struct pci_dev *pdev, +unsigned int decodes); +
+/**
+ *vga_get- acquire & locks VGA resources
+ *
+ * pdev: pci device of the VGA card or NULL for the system
default
+ * rsrc: bit mask of resources to acquire and lock
+ * interruptible: blocking should be interruptible by signals ?
+ *
+ * This function acquires VGA resources for the given
+ * card and mark those resources locked. If the resource
requested
+ * are "normal" (and not legacy) resources, the arbiter will
first check
+ * wether the card is doing legacy decoding for that type of
resource. If
+ * yes, the lock is "converted" into a legacy resource lock.
+ * The arbiter will first look for all VGA cards that might
conflict
+ * and disable their IOs and/or Memory access, inlcuding VGA
forwarding
+ * on P2P bridges if necessary, so that the requested
resources can
+ * be used. Then, the card is marked as locking these
resources and
+ * the IO and/or Memory accesse are enabled on the card
(including
+ * VGA forwarding on parent P2P bridges if any).
+ * This function will block if some conflicting card is
already locking
+ * one of the required resources (or any resource on a
different bus
+ * segment, since P2P bridges don't differenciate VGA memory
and IO
+ * afaik). You can indicate wether this blocking should be
interruptible+ * by a signal (for userland interface) or not. + * Must not be called at interrupt time or in atomic context. + * If the card already owns the resources, the functionsucceeds.
+ * Nested calls are supported (a per-resource counter is
maintained)
+ */include/linux/vgaarb.h new file +3
-0+ +extern int vga_get(struct pci_dev *pdev, unsigned int rsrc, +int interruptible); +
include/linux/vgaarb.h new file +8
-0+/** +* vga_get_interruptible +* +* Shortcut to vga_get +*/ + +static inline int vga_get_interruptible(struct pci_dev *pdev, +unsigned int rsrc) +{
include/linux/vgaarb.h new file +11
-0+return vga_get (pdev, rsrc,1 ); +} + +/** +* vga_get_interruptible +* +* Shortcut to vga_get +*/ + +static inline int vga_get_uninterruptible(struct pci_dev *pdev, +unsigned int rsrc) +{
include/linux/vgaarb.h new file +11
-0+return vga_get (pdev, rsrc,0 ); +} + +/** +* vga_tryget - try to acquire & lock legacy VGA resources +* +* @pdev: pci devivce of VGA card or NULL for system default +* @rsrc: bit mask of resources to acquire and lock +* +* This function performs the same operation as vga_get(), but +* will return an error (-EBUSY) instead of blocking if the resources
+ * are already locked by another card. It can be called in any
context
+ */include/linux/vgaarb.h new file +10
-0+ +extern int vga_tryget (struct pci_dev *pdev,unsigned int rsrc); + +/** +* vga_put - release lock on legacy VGA resources +* +* @pdev: pci device of VGA card or NULL for system default +* @rsrc: but mask of resource to release +* +* This function releases resources previously locked by
vga_get()
+ * orvga_tryget(). The resources aren't disabled right away,
so+ * that a subsequence vga_get() on the same card will succeed + * immediately. Resources have a counter, so locks are only + * released if the counter reaches 0.+ */
include/linux/vgaarb.h new file +8
-0+ +extern void vga_put (struct pci_dev *pdev,unsigned int rsrc); + + +/** +* vga_default_device +* +* This can be defined by the platform. The default implementation
+ * is rather dumb and will probably only work properly on
single
+ * vga card setups and/or x86 platforms.
+ *+ * If your VGA default device is not PCI, you'll have to return + * NULL here. In this case, I assume it will not conflict with + * any PCI card. If this is not true, I'll have to define twoarchs
+ * hooks for enabling/disabling the VGA default device if that
is
+ * possible. This may be a problem with real _ISA_ VGA cards,
in
+ * addition to a PCI one. I don't know at this point how to
deal
+ * with that card. Can theirs IOs be disabled at all ? If not,
then
+ * I suppose it's a matter of having the proper arch hook
telling
+ * us about it, so we basically never allow anybody to succeed
a
+ *vga_get()...
+ */include/linux/vgaarb.h new file +15
-0+ +#ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE +extern struct pci_dev *vga_default_device (void ); +#endif + +/** +* vga_conflicts +* +* Architectures should define this if they have several +* independant PCI domains that can afford concurrent VGA +* decoding +*/ + +#ifndef __ARCH_HAS_VGA_CONFLICT +static inline int vga_conflicts(struct pci_dev *p1, struct pci_dev*p2) +{
include/linux/vgaarb.h new file +22
-0+ return 1; +} +#endif + +/* +* Register a client with the VGA arbitration logic +* return value: number of VGA devices in system. +* +* Clients have two callback mechanisms they can use. +* irq enable/disable callback - +* If a client can't disable its GPUs VGA resources, then we +* need to be able to ask it to turn off its irqs when we +* turn off its mem and io decoding. +* set_vga_decode +* If a client can disable its GPU VGA resource, it will +* get a callback from this to set the encode/decode state +* +* Clients with disable abilities should check the return value +* of this function and if the VGA device count is > 1, should +* disable VGA decoding resources. +* +* Rationale: we cannot disable VGA decode resources unconditionally
+ * some single GPU laptops seem to require ACPI or BIOS access to the
+ * VGA registers to control things like backlights etc.
+ * Hopefully newer multi-GPU laptops do something saner, and desktops
+ * won't have any special ACPI for this.
+ */include/linux/vgaarb.h new file +2
-0+int vga_client_register(struct pci_dev *pdev, void *cookie, + void (*irq_set_state)(void *cookie, boolQuoted 6 lines
state),
+ unsigned int (*set_vga_decode)(void
*cookie, bool state)); +
+#endif /*LINUX_VGA_H*/
--
1.6.0.6Tiago
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment
- and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july
--
_______________________________________________
Dri-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dri-devel
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
Jesse Barnes
AppliedQuoted 6 lines · On Sun, 16 Aug 2009 18:09:36 +0300 Tiago Vignatti <[email protected]>…
Quoted 5 lines · Jesse Barnes
Quoted 2 lines · On Wed, 12 Aug 2009 10:35:16 +0300 "Vignatti Tiago (Nokia-D/Helsinki)"…
Okay, I can handle this documentation pretty easy.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
But honestly I'm not sure we (or some future developer) will use it. The header (
include/linux/vgaarb.h) is already very nice documented. So, tell me what do you think Jesse.As long as it's still accurate we may as well include it.
Here it comes attached. Please review, Jesse.
Applied to my linux-next branch, thanks. If you want the other updates you mentioned to land, you should send a patch for them as well.
Thanks,
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center
PATCH 1/1 PCI/VGA: fix header commentary
In reply to Jesse Barnes---
Amend this somewhere, Jesse. Thanks.
include/linux/vgaarb.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
include/linux/vgaarb.h +1 -1
@@ -1,5 +1,5 @@/* -* vgaarb.c +* vgaarb.h * * (C) Copyright 2005 Benjamin Herrenschmidt <[email protected]> * (C) Copyright 2007 Paulo R. Zanoni <[email protected]>
Signature · 1 line
1.5.6.3
Re: [PATCH] PCI/VGA: fix header commentary
Quoted 19 lines · On Sat, 12 Sep 2009 18:02:31 +0300 Tiago Vignatti <[email protected]>…
Signed-off-by Tiago Vignatti <[email protected]>---
Amend this somewhere, Jesse. Thanks.
include/linux/vgaarb.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)include/linux/vgaarb.h +1
-1@@ -1,5 +1,5 @@/* -* vgaarb.c +* vgaarb.h * * (C) Copyright 2005 Benjamin Herrenschmidt
Quoted 2 lines
<[email protected]>
* (C) Copyright 2007 Paulo R. Zanoni <[email protected]>
I removed the referenced to the filename altogether and replaced it with a short comment. Thanks.
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center