OSDN Git Service

radeon: fix fence race condition hopefully
[android-x86/external-libdrm.git] / linux-core / nv50_fb.c
1 /*
2  * Copyright (C) 2008 Maarten Maathuis.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include "nv50_fb.h"
28 #include "nv50_lut.h"
29 #include "nv50_crtc.h"
30 #include "nv50_display.h"
31
32 static int nv50_fb_bind(struct nv50_crtc *crtc, struct nv50_fb_info *info)
33 {
34         int rval = 0;
35
36         NV50_DEBUG("\n");
37
38         if (!crtc || !info) {
39                 DRM_ERROR("crtc %p info %p\n",crtc, info);
40                 return -EINVAL;
41         }
42
43         if (!info->block || !info->width || !info->height || !info->depth || !info->bpp || !info->pitch) {
44                 DRM_ERROR("block %p width %d height %d depth %d bpp %d pitch %d\n", info->block, info->width, 
45                         info->height, info->depth, info->bpp, info->pitch);
46                 return -EINVAL;
47         }
48
49         crtc->fb->block = info->block;
50         crtc->fb->width = info->width;
51         crtc->fb->height = info->height;
52
53         crtc->fb->y = info->x;
54         crtc->fb->x = info->y;
55
56         crtc->fb->depth = info->depth;
57         crtc->fb->bpp = info->bpp;
58
59         crtc->fb->pitch = info->pitch;
60
61         /* update lut if needed */
62         if (crtc->fb->depth != crtc->lut->depth) {
63                 int r_size = 0, g_size = 0, b_size = 0;
64                 uint16_t *r_val, *g_val, *b_val;
65                 int i;
66
67                 switch (crtc->fb->depth) {
68                         case 15:
69                                 r_size = 32;
70                                 g_size = 32;
71                                 b_size = 32;
72                                 break;
73                         case 16:
74                                 r_size = 32;
75                                 g_size = 64;
76                                 b_size = 32;
77                                 break;
78                         case 24:
79                         default:
80                                 r_size = 256;
81                                 g_size = 256;
82                                 b_size = 256;
83                                 break;
84                 }
85
86                 r_val = kmalloc(r_size * sizeof(uint16_t), GFP_KERNEL);
87                 g_val = kmalloc(g_size * sizeof(uint16_t), GFP_KERNEL);
88                 b_val = kmalloc(b_size * sizeof(uint16_t), GFP_KERNEL);
89
90                 if (!r_val || !g_val || !b_val)
91                         return -ENOMEM;
92
93                 /* Set the color indices. */
94                 for (i = 0; i < r_size; i++) {
95                         r_val[i] = i << 8;
96                 }
97                 for (i = 0; i < g_size; i++) {
98                         g_val[i] = i << 8;
99                 }
100                 for (i = 0; i < b_size; i++) {
101                         b_val[i] = i << 8;
102                 }
103
104                 rval = crtc->lut->set(crtc, r_val, g_val, b_val);
105
106                 /* free before returning */
107                 kfree(r_val);
108                 kfree(g_val);
109                 kfree(b_val);
110
111                 if (rval != 0)
112                         return rval;
113         }
114
115         return 0;
116 }
117
118 int nv50_fb_create(struct nv50_crtc *crtc)
119 {
120         if (!crtc)
121                 return -EINVAL;
122
123         crtc->fb = kzalloc(sizeof(struct nv50_fb), GFP_KERNEL);
124
125         if (!crtc->fb)
126                 return -ENOMEM;
127
128         crtc->fb->bind = nv50_fb_bind;
129
130         return 0;
131 }
132
133 int nv50_fb_destroy(struct nv50_crtc *crtc)
134 {
135         if (!crtc)
136                 return -EINVAL;
137
138         kfree(crtc->fb);
139         crtc->fb = NULL;
140
141         return 0;
142 }