Skip to content

Commit 620ce8b

Browse files
committed
Use native FreeBSD code for mapping PCI resources.
1 parent 9042fba commit 620ce8b

File tree

13 files changed

+116
-140
lines changed

13 files changed

+116
-140
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu.h

+3
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,9 @@ struct amdgpu_device {
20252025
amdgpu_block_wreg_t audio_endpt_wreg;
20262026
void __iomem *rio_mem;
20272027
resource_size_t rio_mem_size;
2028+
#ifdef __FreeBSD__
2029+
int rio_rid;
2030+
#endif
20282031
struct amdgpu_doorbell doorbell;
20292032

20302033
/* clock/pll info */

drivers/gpu/drm/amd/amdgpu/amdgpu_bios.c

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ static bool igp_read_bios_from_vram(struct amdgpu_device *adev)
8080
return true;
8181
}
8282

83+
#ifdef __FreeBSD__
84+
#define pci_map_rom(pdev, sizep) \
85+
vga_pci_map_bios(pdev->dev.bsddev, sizep)
86+
#define pci_unmap_rom(pdev, bios) \
87+
vga_pci_unmap_bios(pdev->dev.bsddev, bios)
88+
#endif
89+
8390
bool amdgpu_read_bios(struct amdgpu_device *adev)
8491
{
8592
uint8_t __iomem *bios, val[2];

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c

+26
Original file line numberDiff line numberDiff line change
@@ -1667,8 +1667,23 @@ int amdgpu_device_init(struct amdgpu_device *adev,
16671667
/* io port mapping */
16681668
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
16691669
if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1670+
#ifdef __FreeBSD__
1671+
struct resource *res;
1672+
int rid;
1673+
1674+
rid = PCIR_BAR(i);
1675+
res = bus_alloc_resource_any(adev->pdev->dev.bsddev,
1676+
SYS_RES_IOPORT, &rid, RF_ACTIVE);
1677+
ddev->drm_pcir[i].res = res;
1678+
ddev->drm_pcir[i].rid = rid;
1679+
1680+
adev->rio_mem = (void *)rman_get_bushandle(res);
1681+
adev->rio_mem_size = rman_get_size(res);
1682+
adev->rio_rid = rid;
1683+
#else
16701684
adev->rio_mem_size = pci_resource_len(adev->pdev, i);
16711685
adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1686+
#endif
16721687
break;
16731688
}
16741689
}
@@ -1882,8 +1897,19 @@ void amdgpu_device_fini(struct amdgpu_device *adev)
18821897
if (adev->flags & AMD_IS_PX)
18831898
vga_switcheroo_fini_domain_pm_ops(adev->dev);
18841899
vga_client_register(adev->pdev, NULL, NULL, NULL);
1900+
#ifdef __FreeBSD__
1901+
if (adev->rio_mem) {
1902+
int rid;
1903+
1904+
rid = adev->rio_rid;
1905+
/* XXX check for error */
1906+
bus_release_resource(adev->pdev->dev.bsddev, SYS_RES_IOPORT,
1907+
rid, adev->ddev->drm_pcir[rid].res);
1908+
}
1909+
#else
18851910
if (adev->rio_mem)
18861911
pci_iounmap(adev->pdev, adev->rio_mem);
1912+
#endif
18871913
adev->rio_mem = NULL;
18881914
iounmap(adev->rmmio);
18891915
adev->rmmio = NULL;

drivers/gpu/drm/i915/i915_drv.c

+24
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,23 @@ static int i915_mmio_setup(struct drm_device *dev)
928928
mmio_size = 512 * 1024;
929929
else
930930
mmio_size = 2 * 1024 * 1024;
931+
#ifdef __FreeBSD__
932+
struct resource *res;
933+
int rid, type;
934+
935+
rid = PCIR_BAR(mmio_bar);
936+
type = pci_resource_type(pdev, mmio_bar);
937+
res = bus_alloc_resource_any(pdev->dev.bsddev, type, &rid, RF_ACTIVE);
938+
939+
dev->drm_pcir[mmio_bar].res = res;
940+
dev->drm_pcir[mmio_bar].rid = rid;
941+
942+
dev_priv->mmio_rid = rid;
943+
dev_priv->mmio_restype = type;
944+
dev_priv->regs = (void *)rman_get_bushandle(res);
945+
#else
931946
dev_priv->regs = pci_iomap(pdev, mmio_bar, mmio_size);
947+
#endif
932948
if (dev_priv->regs == NULL) {
933949
DRM_ERROR("failed to map registers\n");
934950

@@ -947,7 +963,15 @@ static void i915_mmio_cleanup(struct drm_device *dev)
947963
struct pci_dev *pdev = dev_priv->drm.pdev;
948964

949965
intel_teardown_mchbar(dev);
966+
#ifdef __FreeBSD__
967+
int rid;
968+
969+
rid = dev_priv->mmio_rid;
970+
bus_release_resource(pdev->dev.bsddev, dev_priv->mmio_restype,
971+
rid, dev->drm_pcir[rid].res);
972+
#else
950973
pci_iounmap(pdev, dev_priv->regs);
974+
#endif
951975
}
952976

953977
/**

drivers/gpu/drm/i915/i915_drv.h

+4
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,10 @@ struct drm_i915_private {
17591759
int relative_constants_mode;
17601760

17611761
void __iomem *regs;
1762+
#ifdef __FreeBSD__
1763+
int mmio_rid;
1764+
int mmio_restype;
1765+
#endif
17621766

17631767
struct intel_uncore uncore;
17641768

drivers/gpu/drm/i915/intel_bios.c

+7
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,13 @@ static const struct vbt_header *find_vbt(void __iomem *bios, size_t size)
14461446
return NULL;
14471447
}
14481448

1449+
#ifdef __FreeBSD__
1450+
#define pci_map_rom(pdev, sizep) \
1451+
vga_pci_map_bios(pdev->dev.bsddev, sizep)
1452+
#define pci_unmap_rom(pdev, bios) \
1453+
vga_pci_unmap_bios(pdev->dev.bsddev, bios)
1454+
#endif
1455+
14491456
/**
14501457
* intel_bios_init - find VBT and initialize settings from the BIOS
14511458
* @dev_priv: i915 device instance

drivers/gpu/drm/radeon/radeon.h

+3
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,9 @@ struct radeon_device {
23802380
/* io port */
23812381
void __iomem *rio_mem;
23822382
resource_size_t rio_mem_size;
2383+
#ifdef __FreeBSD__
2384+
int rio_rid;
2385+
#endif
23832386
struct radeon_clock clock;
23842387
struct radeon_mc mc;
23852388
struct radeon_gart gart;

drivers/gpu/drm/radeon/radeon_bios.c

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ static bool igp_read_bios_from_vram(struct radeon_device *rdev)
7373
return true;
7474
}
7575

76+
#ifdef __FreeBSD__
77+
#define pci_map_rom(pdev, sizep) \
78+
vga_pci_map_bios(pdev->dev.bsddev, sizep)
79+
#define pci_unmap_rom(pdev, bios) \
80+
vga_pci_unmap_bios(pdev->dev.bsddev, bios)
81+
#endif
82+
7683
static bool radeon_read_bios(struct radeon_device *rdev)
7784
{
7885
uint8_t __iomem *bios, val1, val2;

drivers/gpu/drm/radeon/radeon_device.c

+26
Original file line numberDiff line numberDiff line change
@@ -1464,8 +1464,23 @@ int radeon_device_init(struct radeon_device *rdev,
14641464
/* io port mapping */
14651465
for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
14661466
if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1467+
#ifdef __FreeBSD__
1468+
struct resource *res;
1469+
int rid;
1470+
1471+
rid = PCIR_BAR(i);
1472+
res = bus_alloc_resource_any(rdev->pdev->dev.bsddev,
1473+
SYS_RES_IOPORT, &rid, RF_ACTIVE);
1474+
ddev->drm_pcir[i].res = res;
1475+
ddev->drm_pcir[i].rid = rid;
1476+
1477+
rdev->rio_mem = (void *)rman_get_bushandle(res);
1478+
rdev->rio_mem_size = rman_get_size(res);
1479+
rdev->rio_rid = rid;
1480+
#else
14671481
rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
14681482
rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1483+
#endif
14691484
break;
14701485
}
14711486
}
@@ -1581,8 +1596,19 @@ void radeon_device_fini(struct radeon_device *rdev)
15811596
if (rdev->flags & RADEON_IS_PX)
15821597
vga_switcheroo_fini_domain_pm_ops(rdev->dev);
15831598
vga_client_register(rdev->pdev, NULL, NULL, NULL);
1599+
#ifdef __FreeBSD__
1600+
if (rdev->rio_mem) {
1601+
int rid;
1602+
1603+
rid = rdev->rio_rid;
1604+
/* XXX check for error */
1605+
bus_release_resource(rdev->pdev->dev.bsddev, SYS_RES_IOPORT,
1606+
rid, rdev->ddev->drm_pcir[rid].res);
1607+
}
1608+
#else
15841609
if (rdev->rio_mem)
15851610
pci_iounmap(rdev->pdev, rdev->rio_mem);
1611+
#endif
15861612
rdev->rio_mem = NULL;
15871613
iounmap(rdev->rmmio);
15881614
rdev->rmmio = NULL;

include/drm/drmP.h

+9
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,15 @@ struct drm_device {
989989
int modesetting;
990990

991991
const drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
992+
993+
#ifdef __FreeBSD__
994+
#define DRM_PCI_RESOURCE_MAX 7
995+
996+
struct drm_pci_resource {
997+
struct resource *res;
998+
int rid;
999+
} drm_pcir[DRM_PCI_RESOURCE_MAX];
1000+
#endif
9921001
};
9931002

9941003
#include <drm/drm_irq.h>

sys/compat/linuxkpi/common/include/linux/pci.h

-11
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,13 @@ struct pci_bus {
227227
struct device dev;
228228
};
229229

230-
#define LINUXKPI_BIOS 6
231-
#define LINUXKPI_MAX_PCI_RESOURCE 7
232-
233-
struct pci_resources {
234-
struct resource *r[LINUXKPI_MAX_PCI_RESOURCE];
235-
int rid[LINUXKPI_MAX_PCI_RESOURCE];
236-
void *map[LINUXKPI_MAX_PCI_RESOURCE];
237-
int type[LINUXKPI_MAX_PCI_RESOURCE];
238-
};
239-
240230
struct pci_dev {
241231
struct pci_bus *bus; /* bus this device is on */
242232

243233
struct device dev;
244234

245235
struct list_head links;
246236
struct pci_driver *pdrv;
247-
struct pci_resources pcir;
248237
uint64_t dma_mask;
249238
unsigned int devfn;
250239
uint16_t device;

sys/compat/linuxkpi/gplv2/include/linux/pci.h

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33

44
#include_next <linux/pci.h>
55

6-
extern void pci_unmap_rom(struct pci_dev *pdev, u8 *bios);
7-
extern void *pci_map_rom(struct pci_dev *pdev, size_t *size);
8-
extern void *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max);
9-
extern void pci_iounmap(struct pci_dev *pdev, void *regs);
10-
116
struct pci_dev *linux_pci_get_class(unsigned int class, struct pci_dev *from);
127

138
static inline int

sys/compat/linuxkpi/gplv2/src/linux_device.c

-124
Original file line numberDiff line numberDiff line change
@@ -106,54 +106,6 @@ devres_free(void *res)
106106
}
107107
}
108108

109-
void *
110-
pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
111-
{
112-
struct resource *res;
113-
int rid, len, type;
114-
void *regs;
115-
116-
type = 0;
117-
if (pdev->pcir.r[bar] == NULL) {
118-
rid = PCIR_BAR(bar);
119-
type = pci_resource_type(pdev, bar);
120-
if ((res = bus_alloc_resource_any(pdev->dev.bsddev, type,
121-
&rid, RF_ACTIVE)) == NULL)
122-
return (NULL);
123-
pdev->pcir.r[bar] = res;
124-
pdev->pcir.rid[bar] = rid;
125-
pdev->pcir.type[bar] = type;
126-
regs = (void *)rman_get_bushandle(pdev->pcir.r[bar]);
127-
len = rman_get_end(pdev->pcir.r[bar]) - rman_get_start(pdev->pcir.r[bar]);
128-
129-
pdev->pcir.map[bar] = regs;
130-
131-
}
132-
return (pdev->pcir.map[bar]);
133-
}
134-
135-
void
136-
pci_iounmap(struct pci_dev *pdev, void *regs)
137-
{
138-
int bar, rid, type;
139-
struct resource *res;
140-
141-
res = NULL;
142-
for (bar = 0; bar <= LINUXKPI_MAX_PCI_RESOURCE; bar++) {
143-
if (pdev->pcir.map[bar] != regs)
144-
continue;
145-
res = pdev->pcir.r[bar];
146-
rid = pdev->pcir.rid[bar];
147-
type = pdev->pcir.type[bar];
148-
}
149-
150-
if (res == NULL)
151-
return;
152-
153-
bus_release_resource(pdev->dev.bsddev, type, rid, res);
154-
}
155-
156-
157109
struct pci_dev *
158110
pci_get_bus_and_slot(unsigned int bus, unsigned int devfn)
159111
{
@@ -236,79 +188,3 @@ linux_pci_get_class(unsigned int class, struct pci_dev *from)
236188
pdev->bus->number = pci_get_bus(dev);
237189
return (pdev);
238190
}
239-
240-
static int
241-
is_vga(device_t dev)
242-
{
243-
device_t parent;
244-
devclass_t dc;
245-
246-
parent = device_get_parent(dev);
247-
dc = device_get_devclass(parent);
248-
249-
return (strcmp(devclass_get_name(dc), "vgapci") == 0);
250-
}
251-
252-
void *
253-
pci_map_rom(struct pci_dev *pdev, size_t *size)
254-
{
255-
int rid;
256-
struct resource *res;
257-
device_t dev;
258-
259-
dev = pdev->dev.bsddev;
260-
#if defined(__amd64__) || defined(__i386__)
261-
if (vga_pci_is_boot_display(dev) || is_vga(dev)) {
262-
/*
263-
* On x86, the System BIOS copy the default display
264-
* device's Video BIOS at a fixed location in system
265-
* memory (0xC0000, 128 kBytes long) at boot time.
266-
*
267-
* We use this copy for the default boot device, because
268-
* the original ROM may not be valid after boot.
269-
*/
270-
271-
*size = VGA_PCI_BIOS_SHADOW_SIZE;
272-
return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
273-
}
274-
#endif
275-
276-
rid = PCIR_BIOS;
277-
if ((res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 1, RF_ACTIVE)) == NULL)
278-
return (NULL);
279-
280-
pdev->pcir.r[LINUXKPI_BIOS] = res;
281-
pdev->pcir.rid[LINUXKPI_BIOS] = rid;
282-
pdev->pcir.type[LINUXKPI_BIOS] = SYS_RES_MEMORY;
283-
pdev->pcir.map[LINUXKPI_BIOS] = rman_get_virtual(res);
284-
device_printf(dev, "bios size %lx bios addr %p\n", rman_get_size(res), rman_get_virtual(res));
285-
*size = rman_get_size(res);
286-
return (rman_get_virtual(res));
287-
}
288-
289-
void
290-
pci_unmap_rom(struct pci_dev *pdev, u8 *bios)
291-
{
292-
device_t dev;
293-
struct resource *res;
294-
295-
if (bios == NULL)
296-
return;
297-
dev = pdev->dev.bsddev;
298-
299-
#if defined(__amd64__) || defined(__i386__)
300-
if (vga_pci_is_boot_display(dev) || is_vga(dev)) {
301-
/* We mapped the BIOS shadow copy located at 0xC0000. */
302-
pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
303-
304-
return;
305-
}
306-
#endif
307-
res = pdev->pcir.r[LINUXKPI_BIOS];
308-
pdev->pcir.r[LINUXKPI_BIOS] = NULL;
309-
pdev->pcir.rid[LINUXKPI_BIOS] = -1;
310-
pdev->pcir.type[LINUXKPI_BIOS] = -1;
311-
pdev->pcir.map[LINUXKPI_BIOS] = NULL;
312-
MPASS(res != NULL);
313-
bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BIOS, res);
314-
}

0 commit comments

Comments
 (0)