1945Srugrat /*
2945Srugrat * CDDL HEADER START
3945Srugrat *
4945Srugrat * The contents of this file are subject to the terms of the
51595Srugrat * Common Development and Distribution License (the "License").
61595Srugrat * You may not use this file except in compliance with the License.
7945Srugrat *
8945Srugrat * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9945Srugrat * or http://www.opensolaris.org/os/licensing.
10945Srugrat * See the License for the specific language governing permissions
11945Srugrat * and limitations under the License.
12945Srugrat *
13945Srugrat * When distributing Covered Code, include this CDDL HEADER in each
14945Srugrat * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15945Srugrat * If applicable, add the following below this CDDL HEADER, with the
16945Srugrat * fields enclosed by brackets "[]" replaced with your own identifying
17945Srugrat * information: Portions Copyright [yyyy] [name of copyright owner]
18945Srugrat *
19945Srugrat * CDDL HEADER END
20945Srugrat */
211900Seota
22945Srugrat /*
233987Srugrat * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24945Srugrat * Use is subject to license terms.
25945Srugrat */
26945Srugrat
27945Srugrat #pragma ident "%Z%%M% %I% %E% SMI"
28945Srugrat
29945Srugrat #include <sys/debug.h>
30945Srugrat #include <sys/types.h>
31945Srugrat #include <sys/param.h>
32945Srugrat #include <sys/time.h>
33945Srugrat #include <sys/buf.h>
34945Srugrat #include <sys/errno.h>
35945Srugrat #include <sys/systm.h>
36945Srugrat #include <sys/conf.h>
37945Srugrat #include <sys/signal.h>
38945Srugrat #include <sys/file.h>
39945Srugrat #include <sys/uio.h>
40945Srugrat #include <sys/ioctl.h>
41945Srugrat #include <sys/map.h>
42945Srugrat #include <sys/proc.h>
43945Srugrat #include <sys/user.h>
44945Srugrat #include <sys/mman.h>
45945Srugrat #include <sys/cred.h>
46945Srugrat #include <sys/open.h>
47945Srugrat #include <sys/stat.h>
48945Srugrat #include <sys/utsname.h>
49945Srugrat #include <sys/kmem.h>
50945Srugrat #include <sys/cmn_err.h>
51945Srugrat #include <sys/vnode.h>
52945Srugrat #include <vm/page.h>
53945Srugrat #include <vm/as.h>
54945Srugrat #include <vm/hat.h>
55945Srugrat #include <vm/seg.h>
56945Srugrat #include <vm/seg_kmem.h>
57945Srugrat #include <vm/hat_i86.h>
581595Srugrat #include <sys/vmsystm.h>
59945Srugrat #include <sys/ddi.h>
60945Srugrat #include <sys/devops.h>
61945Srugrat #include <sys/sunddi.h>
62945Srugrat #include <sys/ddi_impldefs.h>
63945Srugrat #include <sys/fs/snode.h>
64945Srugrat #include <sys/pci.h>
65945Srugrat #include <sys/modctl.h>
66945Srugrat #include <sys/uio.h>
67945Srugrat #include <sys/visual_io.h>
68945Srugrat #include <sys/fbio.h>
69945Srugrat #include <sys/ddidmareq.h>
70945Srugrat #include <sys/tnf_probe.h>
71945Srugrat #include <sys/kstat.h>
72945Srugrat #include <sys/callb.h>
73945Srugrat #include <sys/promif.h>
74945Srugrat #include <sys/atomic.h>
75945Srugrat #include "gfx_private.h"
76945Srugrat
77*5084Sjohnlev #ifdef __xpv
78*5084Sjohnlev #include <sys/hypervisor.h>
79*5084Sjohnlev #endif
80*5084Sjohnlev
81945Srugrat /*
82945Srugrat * Create a kva mapping for a pa (start..start+size) with
83945Srugrat * the specified cache attributes (mode).
84945Srugrat */
85945Srugrat gfxp_kva_t
gfxp_map_kernel_space(uint64_t start,size_t size,uint32_t mode)86945Srugrat gfxp_map_kernel_space(uint64_t start, size_t size, uint32_t mode)
87945Srugrat {
88945Srugrat uint_t pgoffset;
89945Srugrat uint64_t base;
90945Srugrat pgcnt_t npages;
91945Srugrat caddr_t cvaddr;
92945Srugrat int hat_flags;
93945Srugrat uint_t hat_attr;
94*5084Sjohnlev pfn_t pfn;
95945Srugrat
96945Srugrat if (size == 0)
97945Srugrat return (0);
98945Srugrat
99*5084Sjohnlev #ifdef __xpv
100*5084Sjohnlev /*
101*5084Sjohnlev * The hypervisor doesn't allow r/w mappings to some pages, such as
102*5084Sjohnlev * page tables, gdt, etc. Detect %cr3 to notify users of this interface.
103*5084Sjohnlev */
104*5084Sjohnlev if (start == mmu_ptob(mmu_btop(getcr3())))
105*5084Sjohnlev return (0);
106*5084Sjohnlev #endif
107*5084Sjohnlev
108945Srugrat if (mode == GFXP_MEMORY_CACHED)
109945Srugrat hat_attr = HAT_STORECACHING_OK;
110945Srugrat else if (mode == GFXP_MEMORY_WRITECOMBINED)
111945Srugrat hat_attr = HAT_MERGING_OK | HAT_PLAT_NOCACHE;
112945Srugrat else /* GFXP_MEMORY_UNCACHED */
113945Srugrat hat_attr = HAT_STRICTORDER | HAT_PLAT_NOCACHE;
114945Srugrat hat_flags = HAT_LOAD_LOCK;
115945Srugrat pgoffset = start & PAGEOFFSET;
116945Srugrat base = start - pgoffset;
117945Srugrat npages = btopr(size + pgoffset);
118945Srugrat cvaddr = vmem_alloc(heap_arena, ptob(npages), VM_NOSLEEP);
119945Srugrat if (cvaddr == NULL)
120945Srugrat return (NULL);
121*5084Sjohnlev
122*5084Sjohnlev #ifdef __xpv
123*5084Sjohnlev ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
124*5084Sjohnlev pfn = xen_assign_pfn(mmu_btop(base));
125*5084Sjohnlev #else
126*5084Sjohnlev pfn = btop(base);
127*5084Sjohnlev #endif
128*5084Sjohnlev
129*5084Sjohnlev hat_devload(kas.a_hat, cvaddr, ptob(npages), pfn,
130*5084Sjohnlev PROT_READ|PROT_WRITE|hat_attr, hat_flags);
131945Srugrat return (cvaddr + pgoffset);
132945Srugrat }
133945Srugrat
134945Srugrat /*
135945Srugrat * Destroy the mapping created by gfxp_map_kernel_space().
136945Srugrat * Physical memory is not reclaimed.
137945Srugrat */
138945Srugrat void
gfxp_unmap_kernel_space(gfxp_kva_t address,size_t size)139945Srugrat gfxp_unmap_kernel_space(gfxp_kva_t address, size_t size)
140945Srugrat {
141945Srugrat uint_t pgoffset;
142945Srugrat caddr_t base;
143945Srugrat pgcnt_t npages;
144945Srugrat
145945Srugrat if (size == 0 || address == NULL)
146945Srugrat return;
147945Srugrat
148945Srugrat pgoffset = (uintptr_t)address & PAGEOFFSET;
149945Srugrat base = (caddr_t)address - pgoffset;
150945Srugrat npages = btopr(size + pgoffset);
151945Srugrat hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
152945Srugrat vmem_free(heap_arena, base, ptob(npages));
153945Srugrat }
154945Srugrat
155945Srugrat /*
156945Srugrat * For a VA return the pfn
157945Srugrat */
158945Srugrat int
gfxp_va2pa(struct as * as,caddr_t addr,uint64_t * pa)159945Srugrat gfxp_va2pa(struct as *as, caddr_t addr, uint64_t *pa)
160945Srugrat {
161*5084Sjohnlev #ifdef __xpv
162*5084Sjohnlev ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
163*5084Sjohnlev *pa = pa_to_ma(pfn_to_pa(hat_getpfnum(as->a_hat, addr)));
164*5084Sjohnlev #else
165*5084Sjohnlev *pa = pfn_to_pa(hat_getpfnum(as->a_hat, addr));
166*5084Sjohnlev #endif
167945Srugrat return (0);
168945Srugrat }
169945Srugrat
170945Srugrat /*
1711900Seota * NOP now
172945Srugrat */
1731900Seota /* ARGSUSED */
174945Srugrat void
gfxp_fix_mem_cache_attrs(caddr_t kva_start,size_t length,int cache_attr)175945Srugrat gfxp_fix_mem_cache_attrs(caddr_t kva_start, size_t length, int cache_attr)
176945Srugrat {
177945Srugrat }
178945Srugrat
179945Srugrat int
gfxp_ddi_dma_mem_alloc(ddi_dma_handle_t handle,size_t length,ddi_device_acc_attr_t * accattrp,uint_t flags,int (* waitfp)(caddr_t),caddr_t arg,caddr_t * kaddrp,size_t * real_length,ddi_acc_handle_t * handlep)180945Srugrat gfxp_ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
181945Srugrat ddi_device_acc_attr_t *accattrp, uint_t flags, int (*waitfp) (caddr_t),
182945Srugrat caddr_t arg, caddr_t *kaddrp, size_t *real_length,
183945Srugrat ddi_acc_handle_t *handlep)
184945Srugrat {
1851900Seota uint_t l_flags = flags & ~IOMEM_DATA_MASK; /* clear cache attrs */
1861900Seota int e;
187945Srugrat
1881900Seota /*
1891900Seota * Set an appropriate attribute from devacc_attr_dataorder
1901900Seota * to keep compatibility. The cache attributes are igonred
1911900Seota * if specified.
1921900Seota */
1931900Seota if (accattrp != NULL) {
1941900Seota if (accattrp->devacc_attr_dataorder == DDI_STRICTORDER_ACC) {
1951900Seota l_flags |= IOMEM_DATA_UNCACHED;
1961900Seota } else if (accattrp->devacc_attr_dataorder ==
1971900Seota DDI_MERGING_OK_ACC) {
1981900Seota l_flags |= IOMEM_DATA_UC_WR_COMBINE;
1991900Seota } else {
2001900Seota l_flags |= IOMEM_DATA_CACHED;
2011900Seota }
202945Srugrat }
203945Srugrat
2041900Seota e = ddi_dma_mem_alloc(handle, length, accattrp, l_flags, waitfp,
2051900Seota arg, kaddrp, real_length, handlep);
2061900Seota return (e);
207945Srugrat }
2081595Srugrat
2091595Srugrat int
gfxp_mlock_user_memory(caddr_t address,size_t length)2101595Srugrat gfxp_mlock_user_memory(caddr_t address, size_t length)
2111595Srugrat {
2121595Srugrat struct as *as = ttoproc(curthread)->p_as;
2131595Srugrat int error = 0;
2141595Srugrat
2151595Srugrat if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
2161595Srugrat return (set_errno(EINVAL));
2171595Srugrat
2181595Srugrat if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
2191595Srugrat RANGE_OKAY)
2201595Srugrat return (set_errno(ENOMEM));
2211595Srugrat
2221595Srugrat error = as_ctl(as, address, length, MC_LOCK, 0, 0, NULL, 0);
2231595Srugrat if (error)
2241595Srugrat (void) set_errno(error);
2251595Srugrat
2261595Srugrat return (error);
2271595Srugrat }
2281595Srugrat
2291595Srugrat int
gfxp_munlock_user_memory(caddr_t address,size_t length)2301595Srugrat gfxp_munlock_user_memory(caddr_t address, size_t length)
2311595Srugrat {
2321595Srugrat struct as *as = ttoproc(curthread)->p_as;
2331595Srugrat int error = 0;
2341595Srugrat
2351595Srugrat if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
2361595Srugrat return (set_errno(EINVAL));
2371595Srugrat
2381595Srugrat if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
2391595Srugrat RANGE_OKAY)
2401595Srugrat return (set_errno(ENOMEM));
2411595Srugrat
2421595Srugrat error = as_ctl(as, address, length, MC_UNLOCK, 0, 0, NULL, 0);
2431595Srugrat if (error)
2441595Srugrat (void) set_errno(error);
2451595Srugrat
2461595Srugrat return (error);
2471595Srugrat }
2483987Srugrat
2493987Srugrat gfx_maddr_t
gfxp_convert_addr(paddr_t paddr)2503987Srugrat gfxp_convert_addr(paddr_t paddr)
2513987Srugrat {
252*5084Sjohnlev #ifdef __xpv
253*5084Sjohnlev ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
254*5084Sjohnlev return (pfn_to_pa(xen_assign_pfn(btop(paddr))));
255*5084Sjohnlev #else
2563987Srugrat return ((gfx_maddr_t)paddr);
257*5084Sjohnlev #endif
2583987Srugrat }
259