1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
8b8e80941Smrg * license, and/or sell copies of the Software, and to permit persons to whom
9b8e80941Smrg * the Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19b8e80941Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20b8e80941Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22b8e80941Smrg
23b8e80941Smrg/* XXX: header order is slightly screwy here */
24b8e80941Smrg#include "loader.h"
25b8e80941Smrg
26b8e80941Smrg#include "adapter9.h"
27b8e80941Smrg
28b8e80941Smrg#include "pipe-loader/pipe_loader.h"
29b8e80941Smrg
30b8e80941Smrg#include "pipe/p_screen.h"
31b8e80941Smrg#include "pipe/p_state.h"
32b8e80941Smrg
33b8e80941Smrg#include "target-helpers/drm_helper.h"
34b8e80941Smrg#include "target-helpers/sw_helper.h"
35b8e80941Smrg#include "state_tracker/drm_driver.h"
36b8e80941Smrg
37b8e80941Smrg#include "d3dadapter/d3dadapter9.h"
38b8e80941Smrg#include "d3dadapter/drm.h"
39b8e80941Smrg
40b8e80941Smrg#include "util/xmlconfig.h"
41b8e80941Smrg#include "util/xmlpool.h"
42b8e80941Smrg
43b8e80941Smrg#include "drm-uapi/drm.h"
44b8e80941Smrg#include <sys/ioctl.h>
45b8e80941Smrg#include <fcntl.h>
46b8e80941Smrg#include <stdio.h>
47b8e80941Smrg
48b8e80941Smrg#define DBG_CHANNEL DBG_ADAPTER
49b8e80941Smrg
50b8e80941Smrgconst char __driConfigOptionsNine[] =
51b8e80941SmrgDRI_CONF_BEGIN
52b8e80941Smrg    DRI_CONF_SECTION_PERFORMANCE
53b8e80941Smrg         DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
54b8e80941Smrg    DRI_CONF_SECTION_END
55b8e80941Smrg    DRI_CONF_SECTION_NINE
56b8e80941Smrg        DRI_CONF_NINE_OVERRIDEVENDOR(-1)
57b8e80941Smrg        DRI_CONF_NINE_THROTTLE(-2)
58b8e80941Smrg        DRI_CONF_NINE_THREADSUBMIT("false")
59b8e80941Smrg        DRI_CONF_NINE_ALLOWDISCARDDELAYEDRELEASE("true")
60b8e80941Smrg        DRI_CONF_NINE_TEARFREEDISCARD("false")
61b8e80941Smrg        DRI_CONF_NINE_CSMT(-1)
62b8e80941Smrg        DRI_CONF_NINE_DYNAMICTEXTUREWORKAROUND("false")
63b8e80941Smrg        DRI_CONF_NINE_SHADERINLINECONSTANTS("false")
64b8e80941Smrg    DRI_CONF_SECTION_END
65b8e80941SmrgDRI_CONF_END;
66b8e80941Smrg
67b8e80941Smrgstruct fallback_card_config {
68b8e80941Smrg    const char *name;
69b8e80941Smrg    unsigned vendor_id;
70b8e80941Smrg    unsigned device_id;
71b8e80941Smrg} fallback_cards[] = {
72b8e80941Smrg        {"NV124", 0x10de, 0x13C2}, /* NVIDIA GeForce GTX 970 */
73b8e80941Smrg        {"HAWAII", 0x1002, 0x67b1}, /* AMD Radeon R9 290 */
74b8e80941Smrg        {"Haswell Mobile", 0x8086, 0x13C2}, /* Intel Haswell Mobile */
75b8e80941Smrg        {"SVGA3D", 0x15ad, 0x0405}, /* VMware SVGA 3D */
76b8e80941Smrg};
77b8e80941Smrg
78b8e80941Smrg/* prototypes */
79b8e80941Smrgvoid
80b8e80941Smrgd3d_match_vendor_id( D3DADAPTER_IDENTIFIER9* drvid,
81b8e80941Smrg                     unsigned fallback_ven,
82b8e80941Smrg                     unsigned fallback_dev,
83b8e80941Smrg                     const char* fallback_name );
84b8e80941Smrg
85b8e80941Smrgvoid d3d_fill_driver_version(D3DADAPTER_IDENTIFIER9* drvid);
86b8e80941Smrg
87b8e80941Smrgvoid d3d_fill_cardname(D3DADAPTER_IDENTIFIER9* drvid);
88b8e80941Smrg
89b8e80941Smrgstruct d3dadapter9drm_context
90b8e80941Smrg{
91b8e80941Smrg    struct d3dadapter9_context base;
92b8e80941Smrg    struct pipe_loader_device *dev, *swdev;
93b8e80941Smrg    int fd;
94b8e80941Smrg};
95b8e80941Smrg
96b8e80941Smrgstatic void
97b8e80941Smrgdrm_destroy( struct d3dadapter9_context *ctx )
98b8e80941Smrg{
99b8e80941Smrg    struct d3dadapter9drm_context *drm = (struct d3dadapter9drm_context *)ctx;
100b8e80941Smrg
101b8e80941Smrg    if (ctx->ref)
102b8e80941Smrg        ctx->ref->destroy(ctx->ref);
103b8e80941Smrg    /* because ref is a wrapper around hal, freeing ref frees hal too. */
104b8e80941Smrg    else if (ctx->hal)
105b8e80941Smrg        ctx->hal->destroy(ctx->hal);
106b8e80941Smrg
107b8e80941Smrg    if (drm->swdev)
108b8e80941Smrg        pipe_loader_release(&drm->swdev, 1);
109b8e80941Smrg    if (drm->dev)
110b8e80941Smrg        pipe_loader_release(&drm->dev, 1);
111b8e80941Smrg
112b8e80941Smrg    close(drm->fd);
113b8e80941Smrg    FREE(ctx);
114b8e80941Smrg}
115b8e80941Smrg
116b8e80941Smrgstatic inline void
117b8e80941Smrgget_bus_info( int fd,
118b8e80941Smrg              DWORD *vendorid,
119b8e80941Smrg              DWORD *deviceid,
120b8e80941Smrg              DWORD *subsysid,
121b8e80941Smrg              DWORD *revision )
122b8e80941Smrg{
123b8e80941Smrg    int vid, did;
124b8e80941Smrg
125b8e80941Smrg    if (loader_get_pci_id_for_fd(fd, &vid, &did)) {
126b8e80941Smrg        DBG("PCI info: vendor=0x%04x, device=0x%04x\n",
127b8e80941Smrg            vid, did);
128b8e80941Smrg        *vendorid = vid;
129b8e80941Smrg        *deviceid = did;
130b8e80941Smrg        *subsysid = 0;
131b8e80941Smrg        *revision = 0;
132b8e80941Smrg    } else {
133b8e80941Smrg        DBG("Unable to detect card. Faking %s\n", fallback_cards[0].name);
134b8e80941Smrg        *vendorid = fallback_cards[0].vendor_id;
135b8e80941Smrg        *deviceid = fallback_cards[0].device_id;
136b8e80941Smrg        *subsysid = 0;
137b8e80941Smrg        *revision = 0;
138b8e80941Smrg    }
139b8e80941Smrg}
140b8e80941Smrg
141b8e80941Smrgstatic inline void
142b8e80941Smrgread_descriptor( struct d3dadapter9_context *ctx,
143b8e80941Smrg                 int fd, int override_vendorid )
144b8e80941Smrg{
145b8e80941Smrg    unsigned i;
146b8e80941Smrg    BOOL found;
147b8e80941Smrg    D3DADAPTER_IDENTIFIER9 *drvid = &ctx->identifier;
148b8e80941Smrg
149b8e80941Smrg    memset(drvid, 0, sizeof(*drvid));
150b8e80941Smrg    get_bus_info(fd, &drvid->VendorId, &drvid->DeviceId,
151b8e80941Smrg                 &drvid->SubSysId, &drvid->Revision);
152b8e80941Smrg    snprintf(drvid->DeviceName, sizeof(drvid->DeviceName),
153b8e80941Smrg                 "Gallium 0.4 with %s", ctx->hal->get_vendor(ctx->hal));
154b8e80941Smrg    snprintf(drvid->Description, sizeof(drvid->Description),
155b8e80941Smrg                 "%s", ctx->hal->get_name(ctx->hal));
156b8e80941Smrg
157b8e80941Smrg    if (override_vendorid > 0) {
158b8e80941Smrg        found = FALSE;
159b8e80941Smrg        /* fill in device_id and card name for fake vendor */
160b8e80941Smrg        for (i = 0; i < sizeof(fallback_cards)/sizeof(fallback_cards[0]); i++) {
161b8e80941Smrg            if (fallback_cards[i].vendor_id == override_vendorid) {
162b8e80941Smrg                DBG("Faking card '%s' vendor 0x%04x, device 0x%04x\n",
163b8e80941Smrg                        fallback_cards[i].name,
164b8e80941Smrg                        fallback_cards[i].vendor_id,
165b8e80941Smrg                        fallback_cards[i].device_id);
166b8e80941Smrg                drvid->VendorId = fallback_cards[i].vendor_id;
167b8e80941Smrg                drvid->DeviceId = fallback_cards[i].device_id;
168b8e80941Smrg                snprintf(drvid->Description, sizeof(drvid->Description),
169b8e80941Smrg                             "%s", fallback_cards[i].name);
170b8e80941Smrg                found = TRUE;
171b8e80941Smrg                break;
172b8e80941Smrg            }
173b8e80941Smrg        }
174b8e80941Smrg        if (!found) {
175b8e80941Smrg            DBG("Unknown fake vendor 0x%04x! Using detected vendor !\n", override_vendorid);
176b8e80941Smrg        }
177b8e80941Smrg    }
178b8e80941Smrg    /* choose fall-back vendor if necessary to allow
179b8e80941Smrg     * the following functions to return sane results */
180b8e80941Smrg    d3d_match_vendor_id(drvid, fallback_cards[0].vendor_id, fallback_cards[0].device_id, fallback_cards[0].name);
181b8e80941Smrg    /* fill in driver name and version info */
182b8e80941Smrg    d3d_fill_driver_version(drvid);
183b8e80941Smrg    /* override Description field with Windows like names */
184b8e80941Smrg    d3d_fill_cardname(drvid);
185b8e80941Smrg
186b8e80941Smrg    /* this driver isn't WHQL certified */
187b8e80941Smrg    drvid->WHQLLevel = 0;
188b8e80941Smrg
189b8e80941Smrg    /* this value is fixed */
190b8e80941Smrg    drvid->DeviceIdentifier.Data1 = 0xaeb2cdd4;
191b8e80941Smrg    drvid->DeviceIdentifier.Data2 = 0x6e41;
192b8e80941Smrg    drvid->DeviceIdentifier.Data3 = 0x43ea;
193b8e80941Smrg    drvid->DeviceIdentifier.Data4[0] = 0x94;
194b8e80941Smrg    drvid->DeviceIdentifier.Data4[1] = 0x1c;
195b8e80941Smrg    drvid->DeviceIdentifier.Data4[2] = 0x83;
196b8e80941Smrg    drvid->DeviceIdentifier.Data4[3] = 0x61;
197b8e80941Smrg    drvid->DeviceIdentifier.Data4[4] = 0xcc;
198b8e80941Smrg    drvid->DeviceIdentifier.Data4[5] = 0x76;
199b8e80941Smrg    drvid->DeviceIdentifier.Data4[6] = 0x07;
200b8e80941Smrg    drvid->DeviceIdentifier.Data4[7] = 0x81;
201b8e80941Smrg}
202b8e80941Smrg
203b8e80941Smrgstatic HRESULT WINAPI
204b8e80941Smrgdrm_create_adapter( int fd,
205b8e80941Smrg                    ID3DAdapter9 **ppAdapter )
206b8e80941Smrg{
207b8e80941Smrg    struct d3dadapter9drm_context *ctx = CALLOC_STRUCT(d3dadapter9drm_context);
208b8e80941Smrg    HRESULT hr;
209b8e80941Smrg    bool different_device;
210b8e80941Smrg    driOptionCache defaultInitOptions;
211b8e80941Smrg    driOptionCache userInitOptions;
212b8e80941Smrg    int throttling_value_user = -2;
213b8e80941Smrg    int override_vendorid = -1;
214b8e80941Smrg
215b8e80941Smrg    if (!ctx) { return E_OUTOFMEMORY; }
216b8e80941Smrg
217b8e80941Smrg    ctx->base.destroy = drm_destroy;
218b8e80941Smrg
219b8e80941Smrg    /* Although the fd is provided from external source, mesa/nine
220b8e80941Smrg     * takes ownership of it. */
221b8e80941Smrg    fd = loader_get_user_preferred_fd(fd, &different_device);
222b8e80941Smrg    ctx->fd = fd;
223b8e80941Smrg    ctx->base.linear_framebuffer = different_device;
224b8e80941Smrg
225b8e80941Smrg    if (!pipe_loader_drm_probe_fd(&ctx->dev, fd)) {
226b8e80941Smrg        ERR("Failed to probe drm fd %d.\n", fd);
227b8e80941Smrg        FREE(ctx);
228b8e80941Smrg        close(fd);
229b8e80941Smrg        return D3DERR_DRIVERINTERNALERROR;
230b8e80941Smrg    }
231b8e80941Smrg
232b8e80941Smrg    ctx->base.hal = pipe_loader_create_screen(ctx->dev);
233b8e80941Smrg    if (!ctx->base.hal) {
234b8e80941Smrg        ERR("Unable to load requested driver.\n");
235b8e80941Smrg        drm_destroy(&ctx->base);
236b8e80941Smrg        return D3DERR_DRIVERINTERNALERROR;
237b8e80941Smrg    }
238b8e80941Smrg
239b8e80941Smrg    if (!ctx->base.hal->get_param(ctx->base.hal, PIPE_CAP_DMABUF)) {
240b8e80941Smrg        ERR("The driver is not capable of dma-buf sharing."
241b8e80941Smrg            "Abandon to load nine state tracker\n");
242b8e80941Smrg        drm_destroy(&ctx->base);
243b8e80941Smrg        return D3DERR_DRIVERINTERNALERROR;
244b8e80941Smrg    }
245b8e80941Smrg
246b8e80941Smrg    /* Previously was set to PIPE_CAP_MAX_FRAMES_IN_FLIGHT,
247b8e80941Smrg     * but the change of value of this cap to 1 seems to cause
248b8e80941Smrg     * regressions. */
249b8e80941Smrg    ctx->base.throttling_value = 2;
250b8e80941Smrg    ctx->base.throttling = ctx->base.throttling_value > 0;
251b8e80941Smrg
252b8e80941Smrg    driParseOptionInfo(&defaultInitOptions, __driConfigOptionsNine);
253b8e80941Smrg    driParseConfigFiles(&userInitOptions, &defaultInitOptions, 0, "nine", NULL);
254b8e80941Smrg    if (driCheckOption(&userInitOptions, "throttle_value", DRI_INT)) {
255b8e80941Smrg        throttling_value_user = driQueryOptioni(&userInitOptions, "throttle_value");
256b8e80941Smrg        if (throttling_value_user == -1)
257b8e80941Smrg            ctx->base.throttling = FALSE;
258b8e80941Smrg        else if (throttling_value_user >= 0) {
259b8e80941Smrg            ctx->base.throttling = TRUE;
260b8e80941Smrg            ctx->base.throttling_value = throttling_value_user;
261b8e80941Smrg        }
262b8e80941Smrg    }
263b8e80941Smrg
264b8e80941Smrg    if (driCheckOption(&userInitOptions, "vblank_mode", DRI_ENUM))
265b8e80941Smrg        ctx->base.vblank_mode = driQueryOptioni(&userInitOptions, "vblank_mode");
266b8e80941Smrg    else
267b8e80941Smrg        ctx->base.vblank_mode = 1;
268b8e80941Smrg
269b8e80941Smrg    if (driCheckOption(&userInitOptions, "thread_submit", DRI_BOOL))
270b8e80941Smrg        ctx->base.thread_submit = driQueryOptionb(&userInitOptions, "thread_submit");
271b8e80941Smrg    else
272b8e80941Smrg        ctx->base.thread_submit = different_device;
273b8e80941Smrg
274b8e80941Smrg    if (driCheckOption(&userInitOptions, "override_vendorid", DRI_INT)) {
275b8e80941Smrg        override_vendorid = driQueryOptioni(&userInitOptions, "override_vendorid");
276b8e80941Smrg    }
277b8e80941Smrg
278b8e80941Smrg    if (driCheckOption(&userInitOptions, "discard_delayed_release", DRI_BOOL))
279b8e80941Smrg        ctx->base.discard_delayed_release = driQueryOptionb(&userInitOptions, "discard_delayed_release");
280b8e80941Smrg    else
281b8e80941Smrg        ctx->base.discard_delayed_release = TRUE;
282b8e80941Smrg
283b8e80941Smrg    if (driCheckOption(&userInitOptions, "tearfree_discard", DRI_BOOL))
284b8e80941Smrg        ctx->base.tearfree_discard = driQueryOptionb(&userInitOptions, "tearfree_discard");
285b8e80941Smrg    else
286b8e80941Smrg        ctx->base.tearfree_discard = FALSE;
287b8e80941Smrg
288b8e80941Smrg    if (ctx->base.tearfree_discard && !ctx->base.discard_delayed_release) {
289b8e80941Smrg        ERR("tearfree_discard requires discard_delayed_release\n");
290b8e80941Smrg        ctx->base.tearfree_discard = FALSE;
291b8e80941Smrg    }
292b8e80941Smrg
293b8e80941Smrg    if (driCheckOption(&userInitOptions, "csmt_force", DRI_INT))
294b8e80941Smrg        ctx->base.csmt_force = driQueryOptioni(&userInitOptions, "csmt_force");
295b8e80941Smrg    else
296b8e80941Smrg        ctx->base.csmt_force = -1;
297b8e80941Smrg
298b8e80941Smrg    if (driCheckOption(&userInitOptions, "dynamic_texture_workaround", DRI_BOOL))
299b8e80941Smrg        ctx->base.dynamic_texture_workaround = driQueryOptionb(&userInitOptions, "dynamic_texture_workaround");
300b8e80941Smrg    else
301b8e80941Smrg        ctx->base.dynamic_texture_workaround = FALSE;
302b8e80941Smrg
303b8e80941Smrg    if (driCheckOption(&userInitOptions, "shader_inline_constants", DRI_BOOL))
304b8e80941Smrg        ctx->base.shader_inline_constants = driQueryOptionb(&userInitOptions, "shader_inline_constants");
305b8e80941Smrg    else
306b8e80941Smrg        ctx->base.shader_inline_constants = FALSE;
307b8e80941Smrg
308b8e80941Smrg    driDestroyOptionCache(&userInitOptions);
309b8e80941Smrg    driDestroyOptionInfo(&defaultInitOptions);
310b8e80941Smrg
311b8e80941Smrg    /* wrap it to create a software screen that can share resources */
312b8e80941Smrg    if (pipe_loader_sw_probe_wrapped(&ctx->swdev, ctx->base.hal))
313b8e80941Smrg        ctx->base.ref = pipe_loader_create_screen(ctx->swdev);
314b8e80941Smrg
315b8e80941Smrg    if (!ctx->base.ref) {
316b8e80941Smrg        ERR("Couldn't wrap drm screen to swrast screen. Software devices "
317b8e80941Smrg            "will be unavailable.\n");
318b8e80941Smrg    }
319b8e80941Smrg
320b8e80941Smrg    /* read out PCI info */
321b8e80941Smrg    read_descriptor(&ctx->base, fd, override_vendorid);
322b8e80941Smrg
323b8e80941Smrg    /* create and return new ID3DAdapter9 */
324b8e80941Smrg    hr = NineAdapter9_new(&ctx->base, (struct NineAdapter9 **)ppAdapter);
325b8e80941Smrg    if (FAILED(hr)) {
326b8e80941Smrg        drm_destroy(&ctx->base);
327b8e80941Smrg        return hr;
328b8e80941Smrg    }
329b8e80941Smrg
330b8e80941Smrg    return D3D_OK;
331b8e80941Smrg}
332b8e80941Smrg
333b8e80941Smrgconst struct D3DAdapter9DRM drm9_desc = {
334b8e80941Smrg    .major_version = D3DADAPTER9DRM_MAJOR,
335b8e80941Smrg    .minor_version = D3DADAPTER9DRM_MINOR,
336b8e80941Smrg    .create_adapter = drm_create_adapter
337b8e80941Smrg};
338