2 * Copyright 2007 Matthieu CASTET <castet.matthieu@free.fr>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
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 * PRECISION INSIGHT AND/OR ITS SUPPLIERS 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 OTHER
22 * DEALINGS IN THE SOFTWARE.
27 #include "nouveau_drv.h"
28 #include "nouveau_drm.h"
30 #define NV20_GRCTX_SIZE (3529)
32 int nv20_graph_context_create(drm_device_t *dev, int channel) {
33 drm_nouveau_private_t *dev_priv =
34 (drm_nouveau_private_t *)dev->dev_private;
35 struct nouveau_fifo *chan = &dev_priv->fifos[channel];
36 unsigned int ctx_size = NV20_GRCTX_SIZE;
39 /* Alloc and clear RAMIN to store the context */
40 chan->ramin_grctx = nouveau_instmem_alloc(dev, ctx_size, 4);
41 if (!chan->ramin_grctx)
42 return DRM_ERR(ENOMEM);
43 for (i=0; i<ctx_size; i+=4)
44 INSTANCE_WR(chan->ramin_grctx, i/4, 0x00000000);
46 /* Initialise default context values */
47 INSTANCE_WR(chan->ramin_grctx, 10, channel << 24); /* CTX_USER */
49 INSTANCE_WR(dev_priv->ctx_table, channel, nouveau_chip_instance_get(dev, chan->ramin_grctx));
54 static void nv20_graph_rdi(drm_device_t *dev) {
55 drm_nouveau_private_t *dev_priv =
56 (drm_nouveau_private_t *)dev->dev_private;
59 NV_WRITE(NV_PGRAPH_RDI_INDEX, 0x2c80000);
60 for (i = 0; i < 32; i++)
61 NV_WRITE(NV_PGRAPH_RDI_DATA, 0);
63 nouveau_wait_for_idle(dev);
66 /* Save current context (from PGRAPH) into the channel's context
68 static void nv20_graph_context_save_current(drm_device_t *dev, int channel) {
69 drm_nouveau_private_t *dev_priv =
70 (drm_nouveau_private_t *)dev->dev_private;
73 instance = INSTANCE_RD(dev_priv->ctx_table, channel);
77 if (instance != nouveau_chip_instance_get(dev, dev_priv->fifos[channel].ramin_grctx))
78 DRM_ERROR("nv20_graph_context_save_current : bad instance\n");
80 NV_WRITE(NV_PGRAPH_CHANNEL_CTX_SIZE, instance);
81 NV_WRITE(NV_PGRAPH_CHANNEL_CTX_POINTER, 2 /* save ctx */);
85 /* Restore the context for a specific channel into PGRAPH
87 static void nv20_graph_context_restore(drm_device_t *dev, int channel) {
88 drm_nouveau_private_t *dev_priv =
89 (drm_nouveau_private_t *)dev->dev_private;
92 instance = INSTANCE_RD(dev_priv->ctx_table, channel);
96 if (instance != nouveau_chip_instance_get(dev, dev_priv->fifos[channel].ramin_grctx))
97 DRM_ERROR("nv20_graph_context_restore_current : bad instance\n");
99 NV_WRITE(NV_PGRAPH_CTX_USER, channel << 24);
100 NV_WRITE(NV_PGRAPH_CHANNEL_CTX_SIZE, instance);
101 NV_WRITE(NV_PGRAPH_CHANNEL_CTX_POINTER, 1 /* restore ctx */);
104 void nouveau_nv20_context_switch(drm_device_t *dev)
106 drm_nouveau_private_t *dev_priv = dev->dev_private;
107 int channel, channel_old;
109 channel=NV_READ(NV_PFIFO_CACH1_PSH1)&(nouveau_fifo_number(dev)-1);
110 channel_old = (NV_READ(NV_PGRAPH_CTX_USER) >> 24) & (nouveau_fifo_number(dev)-1);
112 DRM_INFO("NV: PGRAPH context switch interrupt channel %x -> %x\n",channel_old, channel);
114 NV_WRITE(NV_PGRAPH_FIFO,0x0);
116 nv20_graph_context_save_current(dev, channel_old);
118 nouveau_wait_for_idle(dev);
120 NV_WRITE(NV_PGRAPH_CTX_CONTROL, 0x10000000);
122 nv20_graph_context_restore(dev, channel);
124 nouveau_wait_for_idle(dev);
126 if ((NV_READ(NV_PGRAPH_CTX_USER) >> 24) != channel)
127 DRM_ERROR("nouveau_nv20_context_switch : wrong channel restored %x %x!!!\n", channel, NV_READ(NV_PGRAPH_CTX_USER) >> 24);
129 NV_WRITE(NV_PGRAPH_CTX_CONTROL, 0x10010100);
130 NV_WRITE(NV_PGRAPH_FFINTFC_ST2, NV_READ(NV_PGRAPH_FFINTFC_ST2)&0xCFFFFFFF);
132 NV_WRITE(NV_PGRAPH_FIFO,0x1);
135 int nv20_graph_init(drm_device_t *dev) {
136 drm_nouveau_private_t *dev_priv =
137 (drm_nouveau_private_t *)dev->dev_private;
140 /* Create Context Pointer Table */
141 dev_priv->ctx_table_size = 32 * 4;
142 dev_priv->ctx_table = nouveau_instmem_alloc(dev, dev_priv->ctx_table_size, 4);
143 if (!dev_priv->ctx_table)
144 return DRM_ERR(ENOMEM);
146 for (i=0; i< dev_priv->ctx_table_size; i+=4)
147 INSTANCE_WR(dev_priv->ctx_table, i/4, 0x00000000);
149 NV_WRITE(NV_PGRAPH_CHANNEL_CTX_TABLE, nouveau_chip_instance_get(dev, dev_priv->ctx_table));
151 //XXX need to be done and save/restore for each fifo ???