drm: set permissions on edid file to 0444
[linux-2.6] / drivers / gpu / drm / i915 / intel_bios.c
1 /*
2  * Copyright © 2006 Intel Corporation
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 #include "drmP.h"
28 #include "drm.h"
29 #include "i915_drm.h"
30 #include "i915_drv.h"
31 #include "intel_bios.h"
32
33
34 static void *
35 find_section(struct bdb_header *bdb, int section_id)
36 {
37         u8 *base = (u8 *)bdb;
38         int index = 0;
39         u16 total, current_size;
40         u8 current_id;
41
42         /* skip to first section */
43         index += bdb->header_size;
44         total = bdb->bdb_size;
45
46         /* walk the sections looking for section_id */
47         while (index < total) {
48                 current_id = *(base + index);
49                 index++;
50                 current_size = *((u16 *)(base + index));
51                 index += 2;
52                 if (current_id == section_id)
53                         return base + index;
54                 index += current_size;
55         }
56
57         return NULL;
58 }
59
60 static void
61 fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
62                         struct lvds_dvo_timing *dvo_timing)
63 {
64         panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
65                 dvo_timing->hactive_lo;
66         panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
67                 ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
68         panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
69                 dvo_timing->hsync_pulse_width;
70         panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
71                 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
72
73         panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
74                 dvo_timing->vactive_lo;
75         panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
76                 dvo_timing->vsync_off;
77         panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
78                 dvo_timing->vsync_pulse_width;
79         panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
80                 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
81         panel_fixed_mode->clock = dvo_timing->clock * 10;
82         panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
83
84         /* Some VBTs have bogus h/vtotal values */
85         if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
86                 panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
87         if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
88                 panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
89
90         drm_mode_set_name(panel_fixed_mode);
91 }
92
93 /* Try to find integrated panel data */
94 static void
95 parse_lfp_panel_data(struct drm_i915_private *dev_priv,
96                             struct bdb_header *bdb)
97 {
98         struct bdb_lvds_options *lvds_options;
99         struct bdb_lvds_lfp_data *lvds_lfp_data;
100         struct bdb_lvds_lfp_data_entry *entry;
101         struct lvds_dvo_timing *dvo_timing;
102         struct drm_display_mode *panel_fixed_mode;
103
104         /* Defaults if we can't find VBT info */
105         dev_priv->lvds_dither = 0;
106         dev_priv->lvds_vbt = 0;
107
108         lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
109         if (!lvds_options)
110                 return;
111
112         dev_priv->lvds_dither = lvds_options->pixel_dither;
113         if (lvds_options->panel_type == 0xff)
114                 return;
115
116         lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
117         if (!lvds_lfp_data)
118                 return;
119
120         dev_priv->lvds_vbt = 1;
121
122         entry = &lvds_lfp_data->data[lvds_options->panel_type];
123         dvo_timing = &entry->dvo_timing;
124
125         panel_fixed_mode = drm_calloc(1, sizeof(*panel_fixed_mode),
126                                       DRM_MEM_DRIVER);
127
128         fill_detail_timing_data(panel_fixed_mode, dvo_timing);
129
130         dev_priv->lfp_lvds_vbt_mode = panel_fixed_mode;
131
132         DRM_DEBUG("Found panel mode in BIOS VBT tables:\n");
133         drm_mode_debug_printmodeline(panel_fixed_mode);
134
135         return;
136 }
137
138 /* Try to find sdvo panel data */
139 static void
140 parse_sdvo_panel_data(struct drm_i915_private *dev_priv,
141                       struct bdb_header *bdb)
142 {
143         struct bdb_sdvo_lvds_options *sdvo_lvds_options;
144         struct lvds_dvo_timing *dvo_timing;
145         struct drm_display_mode *panel_fixed_mode;
146
147         dev_priv->sdvo_lvds_vbt_mode = NULL;
148
149         sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
150         if (!sdvo_lvds_options)
151                 return;
152
153         dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
154         if (!dvo_timing)
155                 return;
156
157         panel_fixed_mode = drm_calloc(1, sizeof(*panel_fixed_mode),
158                                       DRM_MEM_DRIVER);
159
160         if (!panel_fixed_mode)
161                 return;
162
163         fill_detail_timing_data(panel_fixed_mode,
164                         dvo_timing + sdvo_lvds_options->panel_type);
165
166         dev_priv->sdvo_lvds_vbt_mode = panel_fixed_mode;
167
168         return;
169 }
170
171 static void
172 parse_general_features(struct drm_i915_private *dev_priv,
173                        struct bdb_header *bdb)
174 {
175         struct bdb_general_features *general;
176
177         /* Set sensible defaults in case we can't find the general block */
178         dev_priv->int_tv_support = 1;
179         dev_priv->int_crt_support = 1;
180
181         general = find_section(bdb, BDB_GENERAL_FEATURES);
182         if (general) {
183                 dev_priv->int_tv_support = general->int_tv_support;
184                 dev_priv->int_crt_support = general->int_crt_support;
185                 dev_priv->lvds_use_ssc = general->enable_ssc;
186
187                 if (dev_priv->lvds_use_ssc) {
188                   if (IS_I855(dev_priv->dev))
189                     dev_priv->lvds_ssc_freq = general->ssc_freq ? 66 : 48;
190                   else
191                     dev_priv->lvds_ssc_freq = general->ssc_freq ? 100 : 96;
192                 }
193         }
194 }
195
196 /**
197  * intel_init_bios - initialize VBIOS settings & find VBT
198  * @dev: DRM device
199  *
200  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
201  * to appropriate values.
202  *
203  * VBT existence is a sanity check that is relied on by other i830_bios.c code.
204  * Note that it would be better to use a BIOS call to get the VBT, as BIOSes may
205  * feed an updated VBT back through that, compared to what we'll fetch using
206  * this method of groping around in the BIOS data.
207  *
208  * Returns 0 on success, nonzero on failure.
209  */
210 bool
211 intel_init_bios(struct drm_device *dev)
212 {
213         struct drm_i915_private *dev_priv = dev->dev_private;
214         struct pci_dev *pdev = dev->pdev;
215         struct vbt_header *vbt = NULL;
216         struct bdb_header *bdb;
217         u8 __iomem *bios;
218         size_t size;
219         int i;
220
221         bios = pci_map_rom(pdev, &size);
222         if (!bios)
223                 return -1;
224
225         /* Scour memory looking for the VBT signature */
226         for (i = 0; i + 4 < size; i++) {
227                 if (!memcmp(bios + i, "$VBT", 4)) {
228                         vbt = (struct vbt_header *)(bios + i);
229                         break;
230                 }
231         }
232
233         if (!vbt) {
234                 DRM_ERROR("VBT signature missing\n");
235                 pci_unmap_rom(pdev, bios);
236                 return -1;
237         }
238
239         bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
240
241         /* Grab useful general definitions */
242         parse_general_features(dev_priv, bdb);
243         parse_lfp_panel_data(dev_priv, bdb);
244         parse_sdvo_panel_data(dev_priv, bdb);
245
246         pci_unmap_rom(pdev, bios);
247
248         return 0;
249 }