Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Red Hat Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * Authors: Ben Skeggs |
| 23 | */ |
| 24 | |
| 25 | #include <linux/firmware.h> |
| 26 | #include "drmP.h" |
| 27 | #include "nouveau_drv.h" |
| 28 | #include "nouveau_util.h" |
Ben Skeggs | 02a841d | 2012-07-04 23:44:54 +1000 | [diff] [blame] | 29 | #include <subdev/vm.h> |
| 30 | #include <core/ramht.h> |
| 31 | #include "fuc/nvc0.fuc.h" |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 32 | |
| 33 | struct nvc0_copy_engine { |
| 34 | struct nouveau_exec_engine base; |
| 35 | u32 irq; |
| 36 | u32 pmc; |
| 37 | u32 fuc; |
| 38 | u32 ctx; |
| 39 | }; |
| 40 | |
Ben Skeggs | 73a60c0 | 2012-07-13 17:21:22 +1000 | [diff] [blame] | 41 | struct nvc0_copy_chan { |
| 42 | struct nouveau_gpuobj *mem; |
| 43 | struct nouveau_vma vma; |
| 44 | }; |
| 45 | |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 46 | static int |
| 47 | nvc0_copy_context_new(struct nouveau_channel *chan, int engine) |
| 48 | { |
| 49 | struct nvc0_copy_engine *pcopy = nv_engine(chan->dev, engine); |
Ben Skeggs | 73a60c0 | 2012-07-13 17:21:22 +1000 | [diff] [blame] | 50 | struct nvc0_copy_chan *cctx; |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 51 | struct drm_device *dev = chan->dev; |
| 52 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 53 | struct nouveau_gpuobj *ramin = chan->ramin; |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 54 | int ret; |
| 55 | |
Ben Skeggs | 73a60c0 | 2012-07-13 17:21:22 +1000 | [diff] [blame] | 56 | cctx = chan->engctx[engine] = kzalloc(sizeof(*cctx), GFP_KERNEL); |
| 57 | if (!cctx) |
| 58 | return -ENOMEM; |
| 59 | |
| 60 | ret = nouveau_gpuobj_new(dev, NULL, 256, 256, |
| 61 | NVOBJ_FLAG_ZERO_ALLOC, &cctx->mem); |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 62 | if (ret) |
| 63 | return ret; |
| 64 | |
Ben Skeggs | 73a60c0 | 2012-07-13 17:21:22 +1000 | [diff] [blame] | 65 | ret = nouveau_gpuobj_map_vm(cctx->mem, NV_MEM_ACCESS_RW, chan->vm, |
| 66 | &cctx->vma); |
| 67 | if (ret) |
| 68 | return ret; |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 69 | |
Ben Skeggs | 73a60c0 | 2012-07-13 17:21:22 +1000 | [diff] [blame] | 70 | nv_wo32(ramin, pcopy->ctx + 0, lower_32_bits(cctx->vma.offset)); |
| 71 | nv_wo32(ramin, pcopy->ctx + 4, upper_32_bits(cctx->vma.offset)); |
| 72 | dev_priv->engine.instmem.flush(dev); |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int |
| 77 | nvc0_copy_object_new(struct nouveau_channel *chan, int engine, |
| 78 | u32 handle, u16 class) |
| 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | nvc0_copy_context_del(struct nouveau_channel *chan, int engine) |
| 85 | { |
| 86 | struct nvc0_copy_engine *pcopy = nv_engine(chan->dev, engine); |
Ben Skeggs | 73a60c0 | 2012-07-13 17:21:22 +1000 | [diff] [blame] | 87 | struct nvc0_copy_chan *cctx = chan->engctx[engine]; |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 88 | struct drm_device *dev = chan->dev; |
| 89 | u32 inst; |
| 90 | |
| 91 | inst = (chan->ramin->vinst >> 12); |
| 92 | inst |= 0x40000000; |
| 93 | |
| 94 | /* disable fifo access */ |
| 95 | nv_wr32(dev, pcopy->fuc + 0x048, 0x00000000); |
| 96 | /* mark channel as unloaded if it's currently active */ |
| 97 | if (nv_rd32(dev, pcopy->fuc + 0x050) == inst) |
| 98 | nv_mask(dev, pcopy->fuc + 0x050, 0x40000000, 0x00000000); |
| 99 | /* mark next channel as invalid if it's about to be loaded */ |
| 100 | if (nv_rd32(dev, pcopy->fuc + 0x054) == inst) |
| 101 | nv_mask(dev, pcopy->fuc + 0x054, 0x40000000, 0x00000000); |
| 102 | /* restore fifo access */ |
| 103 | nv_wr32(dev, pcopy->fuc + 0x048, 0x00000003); |
| 104 | |
| 105 | nv_wo32(chan->ramin, pcopy->ctx + 0, 0x00000000); |
| 106 | nv_wo32(chan->ramin, pcopy->ctx + 4, 0x00000000); |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 107 | |
Ben Skeggs | 73a60c0 | 2012-07-13 17:21:22 +1000 | [diff] [blame] | 108 | nouveau_gpuobj_unmap(&cctx->vma); |
| 109 | nouveau_gpuobj_ref(NULL, &cctx->mem); |
| 110 | |
| 111 | kfree(cctx); |
| 112 | chan->engctx[engine] = NULL; |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int |
| 116 | nvc0_copy_init(struct drm_device *dev, int engine) |
| 117 | { |
| 118 | struct nvc0_copy_engine *pcopy = nv_engine(dev, engine); |
| 119 | int i; |
| 120 | |
| 121 | nv_mask(dev, 0x000200, pcopy->pmc, 0x00000000); |
| 122 | nv_mask(dev, 0x000200, pcopy->pmc, pcopy->pmc); |
| 123 | nv_wr32(dev, pcopy->fuc + 0x014, 0xffffffff); |
| 124 | |
| 125 | nv_wr32(dev, pcopy->fuc + 0x1c0, 0x01000000); |
| 126 | for (i = 0; i < sizeof(nvc0_pcopy_data) / 4; i++) |
| 127 | nv_wr32(dev, pcopy->fuc + 0x1c4, nvc0_pcopy_data[i]); |
| 128 | |
| 129 | nv_wr32(dev, pcopy->fuc + 0x180, 0x01000000); |
| 130 | for (i = 0; i < sizeof(nvc0_pcopy_code) / 4; i++) { |
| 131 | if ((i & 0x3f) == 0) |
| 132 | nv_wr32(dev, pcopy->fuc + 0x188, i >> 6); |
| 133 | nv_wr32(dev, pcopy->fuc + 0x184, nvc0_pcopy_code[i]); |
| 134 | } |
| 135 | |
| 136 | nv_wr32(dev, pcopy->fuc + 0x084, engine - NVOBJ_ENGINE_COPY0); |
| 137 | nv_wr32(dev, pcopy->fuc + 0x10c, 0x00000000); |
| 138 | nv_wr32(dev, pcopy->fuc + 0x104, 0x00000000); /* ENTRY */ |
| 139 | nv_wr32(dev, pcopy->fuc + 0x100, 0x00000002); /* TRIGGER */ |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int |
Ben Skeggs | 6c320fe | 2011-07-20 11:22:33 +1000 | [diff] [blame] | 144 | nvc0_copy_fini(struct drm_device *dev, int engine, bool suspend) |
Ben Skeggs | d5a2737 | 2011-04-01 16:10:08 +1000 | [diff] [blame] | 145 | { |
| 146 | struct nvc0_copy_engine *pcopy = nv_engine(dev, engine); |
| 147 | |
| 148 | nv_mask(dev, pcopy->fuc + 0x048, 0x00000003, 0x00000000); |
| 149 | |
| 150 | /* trigger fuc context unload */ |
| 151 | nv_wait(dev, pcopy->fuc + 0x008, 0x0000000c, 0x00000000); |
| 152 | nv_mask(dev, pcopy->fuc + 0x054, 0x40000000, 0x00000000); |
| 153 | nv_wr32(dev, pcopy->fuc + 0x000, 0x00000008); |
| 154 | nv_wait(dev, pcopy->fuc + 0x008, 0x00000008, 0x00000000); |
| 155 | |
| 156 | nv_wr32(dev, pcopy->fuc + 0x014, 0xffffffff); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static struct nouveau_enum nvc0_copy_isr_error_name[] = { |
| 161 | { 0x0001, "ILLEGAL_MTHD" }, |
| 162 | { 0x0002, "INVALID_ENUM" }, |
| 163 | { 0x0003, "INVALID_BITFIELD" }, |
| 164 | {} |
| 165 | }; |
| 166 | |
| 167 | static void |
| 168 | nvc0_copy_isr(struct drm_device *dev, int engine) |
| 169 | { |
| 170 | struct nvc0_copy_engine *pcopy = nv_engine(dev, engine); |
| 171 | u32 disp = nv_rd32(dev, pcopy->fuc + 0x01c); |
| 172 | u32 stat = nv_rd32(dev, pcopy->fuc + 0x008) & disp & ~(disp >> 16); |
| 173 | u64 inst = (u64)(nv_rd32(dev, pcopy->fuc + 0x050) & 0x0fffffff) << 12; |
| 174 | u32 chid = nvc0_graph_isr_chid(dev, inst); |
| 175 | u32 ssta = nv_rd32(dev, pcopy->fuc + 0x040) & 0x0000ffff; |
| 176 | u32 addr = nv_rd32(dev, pcopy->fuc + 0x040) >> 16; |
| 177 | u32 mthd = (addr & 0x07ff) << 2; |
| 178 | u32 subc = (addr & 0x3800) >> 11; |
| 179 | u32 data = nv_rd32(dev, pcopy->fuc + 0x044); |
| 180 | |
| 181 | if (stat & 0x00000040) { |
| 182 | NV_INFO(dev, "PCOPY: DISPATCH_ERROR ["); |
| 183 | nouveau_enum_print(nvc0_copy_isr_error_name, ssta); |
| 184 | printk("] ch %d [0x%010llx] subc %d mthd 0x%04x data 0x%08x\n", |
| 185 | chid, inst, subc, mthd, data); |
| 186 | nv_wr32(dev, pcopy->fuc + 0x004, 0x00000040); |
| 187 | stat &= ~0x00000040; |
| 188 | } |
| 189 | |
| 190 | if (stat) { |
| 191 | NV_INFO(dev, "PCOPY: unhandled intr 0x%08x\n", stat); |
| 192 | nv_wr32(dev, pcopy->fuc + 0x004, stat); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | nvc0_copy_isr_0(struct drm_device *dev) |
| 198 | { |
| 199 | nvc0_copy_isr(dev, NVOBJ_ENGINE_COPY0); |
| 200 | } |
| 201 | |
| 202 | static void |
| 203 | nvc0_copy_isr_1(struct drm_device *dev) |
| 204 | { |
| 205 | nvc0_copy_isr(dev, NVOBJ_ENGINE_COPY1); |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | nvc0_copy_destroy(struct drm_device *dev, int engine) |
| 210 | { |
| 211 | struct nvc0_copy_engine *pcopy = nv_engine(dev, engine); |
| 212 | |
| 213 | nouveau_irq_unregister(dev, pcopy->irq); |
| 214 | |
| 215 | if (engine == NVOBJ_ENGINE_COPY0) |
| 216 | NVOBJ_ENGINE_DEL(dev, COPY0); |
| 217 | else |
| 218 | NVOBJ_ENGINE_DEL(dev, COPY1); |
| 219 | kfree(pcopy); |
| 220 | } |
| 221 | |
| 222 | int |
| 223 | nvc0_copy_create(struct drm_device *dev, int engine) |
| 224 | { |
| 225 | struct nvc0_copy_engine *pcopy; |
| 226 | |
| 227 | pcopy = kzalloc(sizeof(*pcopy), GFP_KERNEL); |
| 228 | if (!pcopy) |
| 229 | return -ENOMEM; |
| 230 | |
| 231 | pcopy->base.destroy = nvc0_copy_destroy; |
| 232 | pcopy->base.init = nvc0_copy_init; |
| 233 | pcopy->base.fini = nvc0_copy_fini; |
| 234 | pcopy->base.context_new = nvc0_copy_context_new; |
| 235 | pcopy->base.context_del = nvc0_copy_context_del; |
| 236 | pcopy->base.object_new = nvc0_copy_object_new; |
| 237 | |
| 238 | if (engine == 0) { |
| 239 | pcopy->irq = 5; |
| 240 | pcopy->pmc = 0x00000040; |
| 241 | pcopy->fuc = 0x104000; |
| 242 | pcopy->ctx = 0x0230; |
| 243 | nouveau_irq_register(dev, pcopy->irq, nvc0_copy_isr_0); |
| 244 | NVOBJ_ENGINE_ADD(dev, COPY0, &pcopy->base); |
| 245 | NVOBJ_CLASS(dev, 0x90b5, COPY0); |
| 246 | } else { |
| 247 | pcopy->irq = 6; |
| 248 | pcopy->pmc = 0x00000080; |
| 249 | pcopy->fuc = 0x105000; |
| 250 | pcopy->ctx = 0x0240; |
| 251 | nouveau_irq_register(dev, pcopy->irq, nvc0_copy_isr_1); |
| 252 | NVOBJ_ENGINE_ADD(dev, COPY1, &pcopy->base); |
| 253 | NVOBJ_CLASS(dev, 0x90b8, COPY1); |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |