| 1 | /* $NetBSD: nouveau_subdev_fb_nvc0.c,v 1.3 2015/10/14 00:12:55 mrg Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2012 Red Hat Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: Ben Skeggs |
| 25 | */ |
| 26 | |
| 27 | #include <sys/cdefs.h> |
| 28 | __KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_fb_nvc0.c,v 1.3 2015/10/14 00:12:55 mrg Exp $" ); |
| 29 | |
| 30 | #include "nvc0.h" |
| 31 | |
| 32 | extern const u8 nvc0_pte_storage_type_map[256]; |
| 33 | |
| 34 | bool |
| 35 | nvc0_fb_memtype_valid(struct nouveau_fb *pfb, u32 tile_flags) |
| 36 | { |
| 37 | u8 memtype = (tile_flags & 0x0000ff00) >> 8; |
| 38 | return likely((nvc0_pte_storage_type_map[memtype] != 0xff)); |
| 39 | } |
| 40 | |
| 41 | static void |
| 42 | nvc0_fb_intr(struct nouveau_subdev *subdev) |
| 43 | { |
| 44 | struct nvc0_fb_priv *priv = (void *)subdev; |
| 45 | u32 intr = nv_rd32(priv, 0x000100); |
| 46 | if (intr & 0x08000000) { |
| 47 | nv_debug(priv, "PFFB intr\n" ); |
| 48 | intr &= ~0x08000000; |
| 49 | } |
| 50 | if (intr & 0x00002000) { |
| 51 | nv_debug(priv, "PBFB intr\n" ); |
| 52 | intr &= ~0x00002000; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | int |
| 57 | nvc0_fb_init(struct nouveau_object *object) |
| 58 | { |
| 59 | struct nvc0_fb_priv *priv = (void *)object; |
| 60 | int ret; |
| 61 | |
| 62 | ret = nouveau_fb_init(&priv->base); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | |
| 66 | #ifdef __NetBSD__ |
| 67 | if (priv->r100c10_map) |
| 68 | nv_wr32(priv, 0x100c10, priv->r100c10 >> 8); |
| 69 | #else |
| 70 | if (priv->r100c10_page) |
| 71 | nv_wr32(priv, 0x100c10, priv->r100c10 >> 8); |
| 72 | #endif |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | nvc0_fb_dtor(struct nouveau_object *object) |
| 78 | { |
| 79 | struct nouveau_device *device = nv_device(object); |
| 80 | struct nvc0_fb_priv *priv = (void *)object; |
| 81 | |
| 82 | #ifdef __NetBSD__ |
| 83 | if (priv->r100c10_map) { |
| 84 | const bus_dma_tag_t dmat = pci_dma64_available(&device->pdev->pd_pa) ? |
| 85 | device->pdev->pd_pa.pa_dmat64 : device->pdev->pd_pa.pa_dmat; |
| 86 | |
| 87 | bus_dmamap_unload(dmat, priv->r100c10_map); |
| 88 | bus_dmamem_unmap(dmat, priv->r100c10_kva, PAGE_SIZE); |
| 89 | bus_dmamap_destroy(dmat, priv->r100c10_map); |
| 90 | bus_dmamem_free(dmat, &priv->r100c10_seg, 1); |
| 91 | } |
| 92 | #else |
| 93 | if (priv->r100c10_page) { |
| 94 | nv_device_unmap_page(device, priv->r100c10); |
| 95 | __free_page(priv->r100c10_page); |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | nouveau_fb_destroy(&priv->base); |
| 100 | } |
| 101 | |
| 102 | int |
| 103 | nvc0_fb_ctor(struct nouveau_object *parent, struct nouveau_object *engine, |
| 104 | struct nouveau_oclass *oclass, void *data, u32 size, |
| 105 | struct nouveau_object **pobject) |
| 106 | { |
| 107 | struct nouveau_device *device = nv_device(parent); |
| 108 | struct nvc0_fb_priv *priv; |
| 109 | int ret; |
| 110 | |
| 111 | ret = nouveau_fb_create(parent, engine, oclass, &priv); |
| 112 | *pobject = nv_object(priv); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | #ifdef __NetBSD__ |
| 117 | { |
| 118 | const bus_dma_tag_t dmat = pci_dma64_available(&device->pdev->pd_pa) ? |
| 119 | device->pdev->pd_pa.pa_dmat64 : device->pdev->pd_pa.pa_dmat; |
| 120 | int nsegs; |
| 121 | |
| 122 | priv->r100c10_map = NULL; /* paranoia */ |
| 123 | priv->r100c10_kva = NULL; |
| 124 | |
| 125 | /* XXX errno NetBSD->Linux */ |
| 126 | ret = -bus_dmamem_alloc(dmat, PAGE_SIZE, PAGE_SIZE, 0, |
| 127 | &priv->r100c10_seg, 1, &nsegs, BUS_DMA_WAITOK); |
| 128 | if (ret) { |
| 129 | fail0: nouveau_fb_destroy(&priv->base); |
| 130 | return ret; |
| 131 | } |
| 132 | KASSERT(nsegs == 1); |
| 133 | |
| 134 | /* XXX errno NetBSD->Linux */ |
| 135 | ret = -bus_dmamap_create(dmat, PAGE_SIZE, 1, PAGE_SIZE, 0, |
| 136 | BUS_DMA_WAITOK, &priv->r100c10_map); |
| 137 | if (ret) { |
| 138 | fail1: bus_dmamem_free(dmat, &priv->r100c10_seg, 1); |
| 139 | goto fail0; |
| 140 | } |
| 141 | |
| 142 | /* XXX errno NetBSD->Linux */ |
| 143 | ret = -bus_dmamem_map(dmat, &priv->r100c10_seg, 1, PAGE_SIZE, |
| 144 | &priv->r100c10_kva, BUS_DMA_WAITOK); |
| 145 | if (ret) { |
| 146 | fail2: bus_dmamap_destroy(dmat, priv->r100c10_map); |
| 147 | goto fail1; |
| 148 | } |
| 149 | (void)memset(priv->r100c10_kva, 0, PAGE_SIZE); |
| 150 | |
| 151 | /* XXX errno NetBSD->Linux */ |
| 152 | ret = -bus_dmamap_load(dmat, priv->r100c10_map, priv->r100c10_kva, |
| 153 | PAGE_SIZE, NULL, BUS_DMA_WAITOK); |
| 154 | if (ret) { |
| 155 | fail3: __unused bus_dmamem_unmap(dmat, priv->r100c10_kva, PAGE_SIZE); |
| 156 | goto fail2; |
| 157 | } |
| 158 | |
| 159 | priv->r100c10 = priv->r100c10_map->dm_segs[0].ds_addr; |
| 160 | } |
| 161 | #else |
| 162 | priv->r100c10_page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 163 | if (priv->r100c10_page) { |
| 164 | priv->r100c10 = nv_device_map_page(device, priv->r100c10_page); |
| 165 | if (!priv->r100c10) |
| 166 | return -EFAULT; |
| 167 | } |
| 168 | #endif |
| 169 | |
| 170 | nv_subdev(priv)->intr = nvc0_fb_intr; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | struct nouveau_oclass * |
| 175 | nvc0_fb_oclass = &(struct nouveau_fb_impl) { |
| 176 | .base.handle = NV_SUBDEV(FB, 0xc0), |
| 177 | .base.ofuncs = &(struct nouveau_ofuncs) { |
| 178 | .ctor = nvc0_fb_ctor, |
| 179 | .dtor = nvc0_fb_dtor, |
| 180 | .init = nvc0_fb_init, |
| 181 | .fini = _nouveau_fb_fini, |
| 182 | }, |
| 183 | .memtype = nvc0_fb_memtype_valid, |
| 184 | .ram = &nvc0_ram_oclass, |
| 185 | }.base; |
| 186 | |