1/*-
2 * Copyright (c) 2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/11/sys/dev/agp/agp.c 315221 2017-03-14 02:06:03Z pfg $");
29
30#include "opt_agp.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/malloc.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/bus.h>
38#include <sys/conf.h>
39#include <sys/ioccom.h>
40#include <sys/agpio.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/proc.h>
44#include <sys/rwlock.h>
45
46#include <dev/agp/agppriv.h>
47#include <dev/agp/agpvar.h>
48#include <dev/agp/agpreg.h>
49#include <dev/pci/pcivar.h>
50#include <dev/pci/pcireg.h>
51
52#include <vm/vm.h>
53#include <vm/vm_extern.h>
54#include <vm/vm_kern.h>
55#include <vm/vm_param.h>
56#include <vm/vm_object.h>
57#include <vm/vm_page.h>
58#include <vm/vm_pageout.h>
59#include <vm/pmap.h>
60
61#include <machine/bus.h>
62#include <machine/resource.h>
63#include <sys/rman.h>
64
65MODULE_VERSION(agp, 1);
66
67MALLOC_DEFINE(M_AGP, "agp", "AGP data structures");
68
69				/* agp_drv.c */
70static d_open_t agp_open;
71static d_close_t agp_close;
72static d_ioctl_t agp_ioctl;
73static d_mmap_t agp_mmap;
74
75static struct cdevsw agp_cdevsw = {
76	.d_version =	D_VERSION,
77	.d_flags =	D_NEEDGIANT,
78	.d_open =	agp_open,
79	.d_close =	agp_close,
80	.d_ioctl =	agp_ioctl,
81	.d_mmap =	agp_mmap,
82	.d_name =	"agp",
83};
84
85static devclass_t agp_devclass;
86
87/* Helper functions for implementing chipset mini drivers. */
88
89u_int8_t
90agp_find_caps(device_t dev)
91{
92	int capreg;
93
94
95	if (pci_find_cap(dev, PCIY_AGP, &capreg) != 0)
96		capreg = 0;
97	return (capreg);
98}
99
100/*
101 * Find an AGP display device (if any).
102 */
103static device_t
104agp_find_display(void)
105{
106	devclass_t pci = devclass_find("pci");
107	device_t bus, dev = 0;
108	device_t *kids;
109	int busnum, numkids, i;
110
111	for (busnum = 0; busnum < devclass_get_maxunit(pci); busnum++) {
112		bus = devclass_get_device(pci, busnum);
113		if (!bus)
114			continue;
115		if (device_get_children(bus, &kids, &numkids) != 0)
116			continue;
117		for (i = 0; i < numkids; i++) {
118			dev = kids[i];
119			if (pci_get_class(dev) == PCIC_DISPLAY
120			    && pci_get_subclass(dev) == PCIS_DISPLAY_VGA)
121				if (agp_find_caps(dev)) {
122					free(kids, M_TEMP);
123					return dev;
124				}
125
126		}
127		free(kids, M_TEMP);
128	}
129
130	return 0;
131}
132
133struct agp_gatt *
134agp_alloc_gatt(device_t dev)
135{
136	u_int32_t apsize = AGP_GET_APERTURE(dev);
137	u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
138	struct agp_gatt *gatt;
139
140	if (bootverbose)
141		device_printf(dev,
142			      "allocating GATT for aperture of size %dM\n",
143			      apsize / (1024*1024));
144
145	if (entries == 0) {
146		device_printf(dev, "bad aperture size\n");
147		return NULL;
148	}
149
150	gatt = malloc(sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
151	if (!gatt)
152		return 0;
153
154	gatt->ag_entries = entries;
155	gatt->ag_virtual = (void *)kmem_alloc_contig(kernel_arena,
156	    entries * sizeof(u_int32_t), M_NOWAIT | M_ZERO, 0, ~0, PAGE_SIZE,
157	    0, VM_MEMATTR_WRITE_COMBINING);
158	if (!gatt->ag_virtual) {
159		if (bootverbose)
160			device_printf(dev, "contiguous allocation failed\n");
161		free(gatt, M_AGP);
162		return 0;
163	}
164	gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
165
166	return gatt;
167}
168
169void
170agp_free_gatt(struct agp_gatt *gatt)
171{
172	kmem_free(kernel_arena, (vm_offset_t)gatt->ag_virtual,
173	    gatt->ag_entries * sizeof(u_int32_t));
174	free(gatt, M_AGP);
175}
176
177static u_int agp_max[][2] = {
178	{0,	0},
179	{32,	4},
180	{64,	28},
181	{128,	96},
182	{256,	204},
183	{512,	440},
184	{1024,	942},
185	{2048,	1920},
186	{4096,	3932}
187};
188#define	AGP_MAX_SIZE	nitems(agp_max)
189
190/**
191 * Sets the PCI resource which represents the AGP aperture.
192 *
193 * If not called, the default AGP aperture resource of AGP_APBASE will
194 * be used.  Must be called before agp_generic_attach().
195 */
196void
197agp_set_aperture_resource(device_t dev, int rid)
198{
199	struct agp_softc *sc = device_get_softc(dev);
200
201	sc->as_aperture_rid = rid;
202}
203
204int
205agp_generic_attach(device_t dev)
206{
207	struct agp_softc *sc = device_get_softc(dev);
208	int i;
209	u_int memsize;
210
211	/*
212	 * Find and map the aperture, RF_SHAREABLE for DRM but not RF_ACTIVE
213	 * because the kernel doesn't need to map it.
214	 */
215
216	if (sc->as_aperture_rid != -1) {
217		if (sc->as_aperture_rid == 0)
218			sc->as_aperture_rid = AGP_APBASE;
219
220		sc->as_aperture = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
221		    &sc->as_aperture_rid, RF_SHAREABLE);
222		if (!sc->as_aperture)
223			return ENOMEM;
224	}
225
226	/*
227	 * Work out an upper bound for agp memory allocation. This
228	 * uses a heurisitc table from the Linux driver.
229	 */
230	memsize = ptoa(realmem) >> 20;
231	for (i = 0; i < AGP_MAX_SIZE; i++) {
232		if (memsize <= agp_max[i][0])
233			break;
234	}
235	if (i == AGP_MAX_SIZE)
236		i = AGP_MAX_SIZE - 1;
237	sc->as_maxmem = agp_max[i][1] << 20U;
238
239	/*
240	 * The lock is used to prevent re-entry to
241	 * agp_generic_bind_memory() since that function can sleep.
242	 */
243	mtx_init(&sc->as_lock, "agp lock", NULL, MTX_DEF);
244
245	/*
246	 * Initialise stuff for the userland device.
247	 */
248	agp_devclass = devclass_find("agp");
249	TAILQ_INIT(&sc->as_memory);
250	sc->as_nextid = 1;
251
252	sc->as_devnode = make_dev(&agp_cdevsw,
253	    0, UID_ROOT, GID_WHEEL, 0600, "agpgart");
254	sc->as_devnode->si_drv1 = dev;
255
256	return 0;
257}
258
259void
260agp_free_cdev(device_t dev)
261{
262	struct agp_softc *sc = device_get_softc(dev);
263
264	destroy_dev(sc->as_devnode);
265}
266
267void
268agp_free_res(device_t dev)
269{
270	struct agp_softc *sc = device_get_softc(dev);
271
272	if (sc->as_aperture != NULL)
273		bus_release_resource(dev, SYS_RES_MEMORY, sc->as_aperture_rid,
274		    sc->as_aperture);
275	mtx_destroy(&sc->as_lock);
276}
277
278int
279agp_generic_detach(device_t dev)
280{
281
282	agp_free_cdev(dev);
283	agp_free_res(dev);
284	return 0;
285}
286
287/**
288 * Default AGP aperture size detection which simply returns the size of
289 * the aperture's PCI resource.
290 */
291u_int32_t
292agp_generic_get_aperture(device_t dev)
293{
294	struct agp_softc *sc = device_get_softc(dev);
295
296	return rman_get_size(sc->as_aperture);
297}
298
299/**
300 * Default AGP aperture size setting function, which simply doesn't allow
301 * changes to resource size.
302 */
303int
304agp_generic_set_aperture(device_t dev, u_int32_t aperture)
305{
306	u_int32_t current_aperture;
307
308	current_aperture = AGP_GET_APERTURE(dev);
309	if (current_aperture != aperture)
310		return EINVAL;
311	else
312		return 0;
313}
314
315/*
316 * This does the enable logic for v3, with the same topology
317 * restrictions as in place for v2 -- one bus, one device on the bus.
318 */
319static int
320agp_v3_enable(device_t dev, device_t mdev, u_int32_t mode)
321{
322	u_int32_t tstatus, mstatus;
323	u_int32_t command;
324	int rq, sba, fw, rate, arqsz, cal;
325
326	tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
327	mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
328
329	/* Set RQ to the min of mode, tstatus and mstatus */
330	rq = AGP_MODE_GET_RQ(mode);
331	if (AGP_MODE_GET_RQ(tstatus) < rq)
332		rq = AGP_MODE_GET_RQ(tstatus);
333	if (AGP_MODE_GET_RQ(mstatus) < rq)
334		rq = AGP_MODE_GET_RQ(mstatus);
335
336	/*
337	 * ARQSZ - Set the value to the maximum one.
338	 * Don't allow the mode register to override values.
339	 */
340	arqsz = AGP_MODE_GET_ARQSZ(mode);
341	if (AGP_MODE_GET_ARQSZ(tstatus) > rq)
342		rq = AGP_MODE_GET_ARQSZ(tstatus);
343	if (AGP_MODE_GET_ARQSZ(mstatus) > rq)
344		rq = AGP_MODE_GET_ARQSZ(mstatus);
345
346	/* Calibration cycle - don't allow override by mode register */
347	cal = AGP_MODE_GET_CAL(tstatus);
348	if (AGP_MODE_GET_CAL(mstatus) < cal)
349		cal = AGP_MODE_GET_CAL(mstatus);
350
351	/* SBA must be supported for AGP v3. */
352	sba = 1;
353
354	/* Set FW if all three support it. */
355	fw = (AGP_MODE_GET_FW(tstatus)
356	       & AGP_MODE_GET_FW(mstatus)
357	       & AGP_MODE_GET_FW(mode));
358
359	/* Figure out the max rate */
360	rate = (AGP_MODE_GET_RATE(tstatus)
361		& AGP_MODE_GET_RATE(mstatus)
362		& AGP_MODE_GET_RATE(mode));
363	if (rate & AGP_MODE_V3_RATE_8x)
364		rate = AGP_MODE_V3_RATE_8x;
365	else
366		rate = AGP_MODE_V3_RATE_4x;
367	if (bootverbose)
368		device_printf(dev, "Setting AGP v3 mode %d\n", rate * 4);
369
370	pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, 0, 4);
371
372	/* Construct the new mode word and tell the hardware */
373	command = 0;
374	command = AGP_MODE_SET_RQ(0, rq);
375	command = AGP_MODE_SET_ARQSZ(command, arqsz);
376	command = AGP_MODE_SET_CAL(command, cal);
377	command = AGP_MODE_SET_SBA(command, sba);
378	command = AGP_MODE_SET_FW(command, fw);
379	command = AGP_MODE_SET_RATE(command, rate);
380	command = AGP_MODE_SET_MODE_3(command, 1);
381	command = AGP_MODE_SET_AGP(command, 1);
382	pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
383	pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
384
385	return 0;
386}
387
388static int
389agp_v2_enable(device_t dev, device_t mdev, u_int32_t mode)
390{
391	u_int32_t tstatus, mstatus;
392	u_int32_t command;
393	int rq, sba, fw, rate;
394
395	tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
396	mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
397
398	/* Set RQ to the min of mode, tstatus and mstatus */
399	rq = AGP_MODE_GET_RQ(mode);
400	if (AGP_MODE_GET_RQ(tstatus) < rq)
401		rq = AGP_MODE_GET_RQ(tstatus);
402	if (AGP_MODE_GET_RQ(mstatus) < rq)
403		rq = AGP_MODE_GET_RQ(mstatus);
404
405	/* Set SBA if all three can deal with SBA */
406	sba = (AGP_MODE_GET_SBA(tstatus)
407	       & AGP_MODE_GET_SBA(mstatus)
408	       & AGP_MODE_GET_SBA(mode));
409
410	/* Similar for FW */
411	fw = (AGP_MODE_GET_FW(tstatus)
412	       & AGP_MODE_GET_FW(mstatus)
413	       & AGP_MODE_GET_FW(mode));
414
415	/* Figure out the max rate */
416	rate = (AGP_MODE_GET_RATE(tstatus)
417		& AGP_MODE_GET_RATE(mstatus)
418		& AGP_MODE_GET_RATE(mode));
419	if (rate & AGP_MODE_V2_RATE_4x)
420		rate = AGP_MODE_V2_RATE_4x;
421	else if (rate & AGP_MODE_V2_RATE_2x)
422		rate = AGP_MODE_V2_RATE_2x;
423	else
424		rate = AGP_MODE_V2_RATE_1x;
425	if (bootverbose)
426		device_printf(dev, "Setting AGP v2 mode %d\n", rate);
427
428	/* Construct the new mode word and tell the hardware */
429	command = 0;
430	command = AGP_MODE_SET_RQ(0, rq);
431	command = AGP_MODE_SET_SBA(command, sba);
432	command = AGP_MODE_SET_FW(command, fw);
433	command = AGP_MODE_SET_RATE(command, rate);
434	command = AGP_MODE_SET_AGP(command, 1);
435	pci_write_config(dev, agp_find_caps(dev) + AGP_COMMAND, command, 4);
436	pci_write_config(mdev, agp_find_caps(mdev) + AGP_COMMAND, command, 4);
437
438	return 0;
439}
440
441int
442agp_generic_enable(device_t dev, u_int32_t mode)
443{
444	device_t mdev = agp_find_display();
445	u_int32_t tstatus, mstatus;
446
447	if (!mdev) {
448		AGP_DPF("can't find display\n");
449		return ENXIO;
450	}
451
452	tstatus = pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
453	mstatus = pci_read_config(mdev, agp_find_caps(mdev) + AGP_STATUS, 4);
454
455	/*
456	 * Check display and bridge for AGP v3 support.  AGP v3 allows
457	 * more variety in topology than v2, e.g. multiple AGP devices
458	 * attached to one bridge, or multiple AGP bridges in one
459	 * system.  This doesn't attempt to address those situations,
460	 * but should work fine for a classic single AGP slot system
461	 * with AGP v3.
462	 */
463	if (AGP_MODE_GET_MODE_3(mode) &&
464	    AGP_MODE_GET_MODE_3(tstatus) &&
465	    AGP_MODE_GET_MODE_3(mstatus))
466		return (agp_v3_enable(dev, mdev, mode));
467	else
468		return (agp_v2_enable(dev, mdev, mode));
469}
470
471struct agp_memory *
472agp_generic_alloc_memory(device_t dev, int type, vm_size_t size)
473{
474	struct agp_softc *sc = device_get_softc(dev);
475	struct agp_memory *mem;
476
477	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
478		return 0;
479
480	if (size > sc->as_maxmem - sc->as_allocated)
481		return 0;
482
483	if (type != 0) {
484		printf("agp_generic_alloc_memory: unsupported type %d\n",
485		       type);
486		return 0;
487	}
488
489	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
490	mem->am_id = sc->as_nextid++;
491	mem->am_size = size;
492	mem->am_type = 0;
493	mem->am_obj = vm_object_allocate(OBJT_DEFAULT, atop(round_page(size)));
494	mem->am_physical = 0;
495	mem->am_offset = 0;
496	mem->am_is_bound = 0;
497	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
498	sc->as_allocated += size;
499
500	return mem;
501}
502
503int
504agp_generic_free_memory(device_t dev, struct agp_memory *mem)
505{
506	struct agp_softc *sc = device_get_softc(dev);
507
508	if (mem->am_is_bound)
509		return EBUSY;
510
511	sc->as_allocated -= mem->am_size;
512	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
513	vm_object_deallocate(mem->am_obj);
514	free(mem, M_AGP);
515	return 0;
516}
517
518int
519agp_generic_bind_memory(device_t dev, struct agp_memory *mem,
520			vm_offset_t offset)
521{
522	struct agp_softc *sc = device_get_softc(dev);
523	vm_offset_t i, j, k;
524	vm_page_t m;
525	int error;
526
527	/* Do some sanity checks first. */
528	if ((offset & (AGP_PAGE_SIZE - 1)) != 0 ||
529	    offset + mem->am_size > AGP_GET_APERTURE(dev)) {
530		device_printf(dev, "binding memory at bad offset %#x\n",
531		    (int)offset);
532		return EINVAL;
533	}
534
535	/*
536	 * Allocate the pages early, before acquiring the lock,
537	 * because vm_page_grab() may sleep and we can't hold a mutex
538	 * while sleeping.
539	 */
540	VM_OBJECT_WLOCK(mem->am_obj);
541	for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
542		/*
543		 * Find a page from the object and wire it
544		 * down. This page will be mapped using one or more
545		 * entries in the GATT (assuming that PAGE_SIZE >=
546		 * AGP_PAGE_SIZE. If this is the first call to bind,
547		 * the pages will be allocated and zeroed.
548		 */
549		m = vm_page_grab(mem->am_obj, OFF_TO_IDX(i),
550		    VM_ALLOC_WIRED | VM_ALLOC_ZERO);
551		AGP_DPF("found page pa=%#jx\n", (uintmax_t)VM_PAGE_TO_PHYS(m));
552	}
553	VM_OBJECT_WUNLOCK(mem->am_obj);
554
555	mtx_lock(&sc->as_lock);
556
557	if (mem->am_is_bound) {
558		device_printf(dev, "memory already bound\n");
559		error = EINVAL;
560		VM_OBJECT_WLOCK(mem->am_obj);
561		i = 0;
562		goto bad;
563	}
564
565	/*
566	 * Bind the individual pages and flush the chipset's
567	 * TLB.
568	 */
569	VM_OBJECT_WLOCK(mem->am_obj);
570	for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
571		m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(i));
572
573		/*
574		 * Install entries in the GATT, making sure that if
575		 * AGP_PAGE_SIZE < PAGE_SIZE and mem->am_size is not
576		 * aligned to PAGE_SIZE, we don't modify too many GATT
577		 * entries.
578		 */
579		for (j = 0; j < PAGE_SIZE && i + j < mem->am_size;
580		     j += AGP_PAGE_SIZE) {
581			vm_offset_t pa = VM_PAGE_TO_PHYS(m) + j;
582			AGP_DPF("binding offset %#jx to pa %#jx\n",
583				(uintmax_t)offset + i + j, (uintmax_t)pa);
584			error = AGP_BIND_PAGE(dev, offset + i + j, pa);
585			if (error) {
586				/*
587				 * Bail out. Reverse all the mappings
588				 * and unwire the pages.
589				 */
590				for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
591					AGP_UNBIND_PAGE(dev, offset + k);
592				goto bad;
593			}
594		}
595		vm_page_xunbusy(m);
596	}
597	VM_OBJECT_WUNLOCK(mem->am_obj);
598
599	/*
600	 * Make sure the chipset gets the new mappings.
601	 */
602	AGP_FLUSH_TLB(dev);
603
604	mem->am_offset = offset;
605	mem->am_is_bound = 1;
606
607	mtx_unlock(&sc->as_lock);
608
609	return 0;
610bad:
611	mtx_unlock(&sc->as_lock);
612	VM_OBJECT_ASSERT_WLOCKED(mem->am_obj);
613	for (k = 0; k < mem->am_size; k += PAGE_SIZE) {
614		m = vm_page_lookup(mem->am_obj, OFF_TO_IDX(k));
615		if (k >= i)
616			vm_page_xunbusy(m);
617		vm_page_lock(m);
618		vm_page_unwire(m, PQ_INACTIVE);
619		vm_page_unlock(m);
620	}
621	VM_OBJECT_WUNLOCK(mem->am_obj);
622
623	return error;
624}
625
626int
627agp_generic_unbind_memory(device_t dev, struct agp_memory *mem)
628{
629	struct agp_softc *sc = device_get_softc(dev);
630	vm_page_t m;
631	int i;
632
633	mtx_lock(&sc->as_lock);
634
635	if (!mem->am_is_bound) {
636		device_printf(dev, "memory is not bound\n");
637		mtx_unlock(&sc->as_lock);
638		return EINVAL;
639	}
640
641
642	/*
643	 * Unbind the individual pages and flush the chipset's
644	 * TLB. Unwire the pages so they can be swapped.
645	 */
646	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
647		AGP_UNBIND_PAGE(dev, mem->am_offset + i);
648
649	AGP_FLUSH_TLB(dev);
650
651	VM_OBJECT_WLOCK(mem->am_obj);
652	for (i = 0; i < mem->am_size; i += PAGE_SIZE) {
653		m = vm_page_lookup(mem->am_obj, atop(i));
654		vm_page_lock(m);
655		vm_page_unwire(m, PQ_INACTIVE);
656		vm_page_unlock(m);
657	}
658	VM_OBJECT_WUNLOCK(mem->am_obj);
659
660	mem->am_offset = 0;
661	mem->am_is_bound = 0;
662
663	mtx_unlock(&sc->as_lock);
664
665	return 0;
666}
667
668/* Helper functions for implementing user/kernel api */
669
670static int
671agp_acquire_helper(device_t dev, enum agp_acquire_state state)
672{
673	struct agp_softc *sc = device_get_softc(dev);
674
675	if (sc->as_state != AGP_ACQUIRE_FREE)
676		return EBUSY;
677	sc->as_state = state;
678
679	return 0;
680}
681
682static int
683agp_release_helper(device_t dev, enum agp_acquire_state state)
684{
685	struct agp_softc *sc = device_get_softc(dev);
686
687	if (sc->as_state == AGP_ACQUIRE_FREE)
688		return 0;
689
690	if (sc->as_state != state)
691		return EBUSY;
692
693	sc->as_state = AGP_ACQUIRE_FREE;
694	return 0;
695}
696
697static struct agp_memory *
698agp_find_memory(device_t dev, int id)
699{
700	struct agp_softc *sc = device_get_softc(dev);
701	struct agp_memory *mem;
702
703	AGP_DPF("searching for memory block %d\n", id);
704	TAILQ_FOREACH(mem, &sc->as_memory, am_link) {
705		AGP_DPF("considering memory block %d\n", mem->am_id);
706		if (mem->am_id == id)
707			return mem;
708	}
709	return 0;
710}
711
712/* Implementation of the userland ioctl api */
713
714static int
715agp_info_user(device_t dev, agp_info *info)
716{
717	struct agp_softc *sc = device_get_softc(dev);
718
719	bzero(info, sizeof *info);
720	info->bridge_id = pci_get_devid(dev);
721	info->agp_mode =
722	    pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
723	if (sc->as_aperture)
724		info->aper_base = rman_get_start(sc->as_aperture);
725	else
726		info->aper_base = 0;
727	info->aper_size = AGP_GET_APERTURE(dev) >> 20;
728	info->pg_total = info->pg_system = sc->as_maxmem >> AGP_PAGE_SHIFT;
729	info->pg_used = sc->as_allocated >> AGP_PAGE_SHIFT;
730
731	return 0;
732}
733
734static int
735agp_setup_user(device_t dev, agp_setup *setup)
736{
737	return AGP_ENABLE(dev, setup->agp_mode);
738}
739
740static int
741agp_allocate_user(device_t dev, agp_allocate *alloc)
742{
743	struct agp_memory *mem;
744
745	mem = AGP_ALLOC_MEMORY(dev,
746			       alloc->type,
747			       alloc->pg_count << AGP_PAGE_SHIFT);
748	if (mem) {
749		alloc->key = mem->am_id;
750		alloc->physical = mem->am_physical;
751		return 0;
752	} else {
753		return ENOMEM;
754	}
755}
756
757static int
758agp_deallocate_user(device_t dev, int id)
759{
760	struct agp_memory *mem = agp_find_memory(dev, id);
761
762	if (mem) {
763		AGP_FREE_MEMORY(dev, mem);
764		return 0;
765	} else {
766		return ENOENT;
767	}
768}
769
770static int
771agp_bind_user(device_t dev, agp_bind *bind)
772{
773	struct agp_memory *mem = agp_find_memory(dev, bind->key);
774
775	if (!mem)
776		return ENOENT;
777
778	return AGP_BIND_MEMORY(dev, mem, bind->pg_start << AGP_PAGE_SHIFT);
779}
780
781static int
782agp_unbind_user(device_t dev, agp_unbind *unbind)
783{
784	struct agp_memory *mem = agp_find_memory(dev, unbind->key);
785
786	if (!mem)
787		return ENOENT;
788
789	return AGP_UNBIND_MEMORY(dev, mem);
790}
791
792static int
793agp_chipset_flush(device_t dev)
794{
795
796	return (AGP_CHIPSET_FLUSH(dev));
797}
798
799static int
800agp_open(struct cdev *kdev, int oflags, int devtype, struct thread *td)
801{
802	device_t dev = kdev->si_drv1;
803	struct agp_softc *sc = device_get_softc(dev);
804
805	if (!sc->as_isopen) {
806		sc->as_isopen = 1;
807		device_busy(dev);
808	}
809
810	return 0;
811}
812
813static int
814agp_close(struct cdev *kdev, int fflag, int devtype, struct thread *td)
815{
816	device_t dev = kdev->si_drv1;
817	struct agp_softc *sc = device_get_softc(dev);
818	struct agp_memory *mem;
819
820	/*
821	 * Clear the GATT and force release on last close
822	 */
823	while ((mem = TAILQ_FIRST(&sc->as_memory)) != NULL) {
824		if (mem->am_is_bound)
825			AGP_UNBIND_MEMORY(dev, mem);
826		AGP_FREE_MEMORY(dev, mem);
827	}
828	if (sc->as_state == AGP_ACQUIRE_USER)
829		agp_release_helper(dev, AGP_ACQUIRE_USER);
830	sc->as_isopen = 0;
831	device_unbusy(dev);
832
833	return 0;
834}
835
836static int
837agp_ioctl(struct cdev *kdev, u_long cmd, caddr_t data, int fflag, struct thread *td)
838{
839	device_t dev = kdev->si_drv1;
840
841	switch (cmd) {
842	case AGPIOC_INFO:
843		return agp_info_user(dev, (agp_info *) data);
844
845	case AGPIOC_ACQUIRE:
846		return agp_acquire_helper(dev, AGP_ACQUIRE_USER);
847
848	case AGPIOC_RELEASE:
849		return agp_release_helper(dev, AGP_ACQUIRE_USER);
850
851	case AGPIOC_SETUP:
852		return agp_setup_user(dev, (agp_setup *)data);
853
854	case AGPIOC_ALLOCATE:
855		return agp_allocate_user(dev, (agp_allocate *)data);
856
857	case AGPIOC_DEALLOCATE:
858		return agp_deallocate_user(dev, *(int *) data);
859
860	case AGPIOC_BIND:
861		return agp_bind_user(dev, (agp_bind *)data);
862
863	case AGPIOC_UNBIND:
864		return agp_unbind_user(dev, (agp_unbind *)data);
865
866	case AGPIOC_CHIPSET_FLUSH:
867		return agp_chipset_flush(dev);
868	}
869
870	return EINVAL;
871}
872
873static int
874agp_mmap(struct cdev *kdev, vm_ooffset_t offset, vm_paddr_t *paddr,
875    int prot, vm_memattr_t *memattr)
876{
877	device_t dev = kdev->si_drv1;
878	struct agp_softc *sc = device_get_softc(dev);
879
880	if (offset > AGP_GET_APERTURE(dev))
881		return -1;
882	if (sc->as_aperture == NULL)
883		return -1;
884	*paddr = rman_get_start(sc->as_aperture) + offset;
885	return 0;
886}
887
888/* Implementation of the kernel api */
889
890device_t
891agp_find_device()
892{
893	device_t *children, child;
894	int i, count;
895
896	if (!agp_devclass)
897		return NULL;
898	if (devclass_get_devices(agp_devclass, &children, &count) != 0)
899		return NULL;
900	child = NULL;
901	for (i = 0; i < count; i++) {
902		if (device_is_attached(children[i])) {
903			child = children[i];
904			break;
905		}
906	}
907	free(children, M_TEMP);
908	return child;
909}
910
911enum agp_acquire_state
912agp_state(device_t dev)
913{
914	struct agp_softc *sc = device_get_softc(dev);
915	return sc->as_state;
916}
917
918void
919agp_get_info(device_t dev, struct agp_info *info)
920{
921	struct agp_softc *sc = device_get_softc(dev);
922
923	info->ai_mode =
924		pci_read_config(dev, agp_find_caps(dev) + AGP_STATUS, 4);
925	if (sc->as_aperture != NULL)
926		info->ai_aperture_base = rman_get_start(sc->as_aperture);
927	else
928		info->ai_aperture_base = 0;
929	info->ai_aperture_size = AGP_GET_APERTURE(dev);
930	info->ai_memory_allowed = sc->as_maxmem;
931	info->ai_memory_used = sc->as_allocated;
932}
933
934int
935agp_acquire(device_t dev)
936{
937	return agp_acquire_helper(dev, AGP_ACQUIRE_KERNEL);
938}
939
940int
941agp_release(device_t dev)
942{
943	return agp_release_helper(dev, AGP_ACQUIRE_KERNEL);
944}
945
946int
947agp_enable(device_t dev, u_int32_t mode)
948{
949	return AGP_ENABLE(dev, mode);
950}
951
952void *agp_alloc_memory(device_t dev, int type, vm_size_t bytes)
953{
954	return  (void *) AGP_ALLOC_MEMORY(dev, type, bytes);
955}
956
957void agp_free_memory(device_t dev, void *handle)
958{
959	struct agp_memory *mem = (struct agp_memory *) handle;
960	AGP_FREE_MEMORY(dev, mem);
961}
962
963int agp_bind_memory(device_t dev, void *handle, vm_offset_t offset)
964{
965	struct agp_memory *mem = (struct agp_memory *) handle;
966	return AGP_BIND_MEMORY(dev, mem, offset);
967}
968
969int agp_unbind_memory(device_t dev, void *handle)
970{
971	struct agp_memory *mem = (struct agp_memory *) handle;
972	return AGP_UNBIND_MEMORY(dev, mem);
973}
974
975void agp_memory_info(device_t dev, void *handle, struct
976		     agp_memory_info *mi)
977{
978	struct agp_memory *mem = (struct agp_memory *) handle;
979
980	mi->ami_size = mem->am_size;
981	mi->ami_physical = mem->am_physical;
982	mi->ami_offset = mem->am_offset;
983	mi->ami_is_bound = mem->am_is_bound;
984}
985
986int
987agp_bind_pages(device_t dev, vm_page_t *pages, vm_size_t size,
988    vm_offset_t offset)
989{
990	struct agp_softc *sc;
991	vm_offset_t i, j, k, pa;
992	vm_page_t m;
993	int error;
994
995	if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
996	    (offset & (AGP_PAGE_SIZE - 1)) != 0)
997		return (EINVAL);
998
999	sc = device_get_softc(dev);
1000
1001	mtx_lock(&sc->as_lock);
1002	for (i = 0; i < size; i += PAGE_SIZE) {
1003		m = pages[OFF_TO_IDX(i)];
1004		KASSERT(m->wire_count > 0,
1005		    ("agp_bind_pages: page %p hasn't been wired", m));
1006
1007		/*
1008		 * Install entries in the GATT, making sure that if
1009		 * AGP_PAGE_SIZE < PAGE_SIZE and size is not
1010		 * aligned to PAGE_SIZE, we don't modify too many GATT
1011		 * entries.
1012		 */
1013		for (j = 0; j < PAGE_SIZE && i + j < size; j += AGP_PAGE_SIZE) {
1014			pa = VM_PAGE_TO_PHYS(m) + j;
1015			AGP_DPF("binding offset %#jx to pa %#jx\n",
1016				(uintmax_t)offset + i + j, (uintmax_t)pa);
1017			error = AGP_BIND_PAGE(dev, offset + i + j, pa);
1018			if (error) {
1019				/*
1020				 * Bail out. Reverse all the mappings.
1021				 */
1022				for (k = 0; k < i + j; k += AGP_PAGE_SIZE)
1023					AGP_UNBIND_PAGE(dev, offset + k);
1024
1025				mtx_unlock(&sc->as_lock);
1026				return (error);
1027			}
1028		}
1029	}
1030
1031	AGP_FLUSH_TLB(dev);
1032
1033	mtx_unlock(&sc->as_lock);
1034	return (0);
1035}
1036
1037int
1038agp_unbind_pages(device_t dev, vm_size_t size, vm_offset_t offset)
1039{
1040	struct agp_softc *sc;
1041	vm_offset_t i;
1042
1043	if ((size & (AGP_PAGE_SIZE - 1)) != 0 ||
1044	    (offset & (AGP_PAGE_SIZE - 1)) != 0)
1045		return (EINVAL);
1046
1047	sc = device_get_softc(dev);
1048
1049	mtx_lock(&sc->as_lock);
1050	for (i = 0; i < size; i += AGP_PAGE_SIZE)
1051		AGP_UNBIND_PAGE(dev, offset + i);
1052
1053	AGP_FLUSH_TLB(dev);
1054
1055	mtx_unlock(&sc->as_lock);
1056	return (0);
1057}
1058