1/* wld: intel/batch.c
2 *
3 * Copyright (c) 2013, 2014 Michael Forney
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the 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 THE
18 * 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
24#include "batch.h"
25#include "mi.h"
26
27#include <i915_drm.h>
28#include <stdlib.h>
29
30static const struct intel_device_info device_info_i965 = { .gen = 4 };
31static const struct intel_device_info device_info_g4x = { .gen = 4 };
32static const struct intel_device_info device_info_ilk = { .gen = 5 };
33static const struct intel_device_info device_info_snb_gt1 = { .gen = 6 };
34static const struct intel_device_info device_info_snb_gt2 = { .gen = 6 };
35static const struct intel_device_info device_info_ivb_gt1 = { .gen = 7 };
36static const struct intel_device_info device_info_ivb_gt2 = { .gen = 7 };
37static const struct intel_device_info device_info_byt = { .gen = 7 };
38static const struct intel_device_info device_info_hsw_gt1 = { .gen = 7 };
39static const struct intel_device_info device_info_hsw_gt2 = { .gen = 7 };
40static const struct intel_device_info device_info_hsw_gt3 = { .gen = 7 };
41static const struct intel_device_info device_info_bdw_gt1 = { .gen = 8 };
42static const struct intel_device_info device_info_bdw_gt2 = { .gen = 8 };
43static const struct intel_device_info device_info_bdw_gt3 = { .gen = 8 };
44static const struct intel_device_info device_info_bdw_rsvd = { .gen = 8 };
45static const struct intel_device_info device_info_chv = { .gen = 8 };
46static const struct intel_device_info device_info_skl_gt1 = { .gen = 9 };
47static const struct intel_device_info device_info_skl_gt2 = { .gen = 9 };
48static const struct intel_device_info device_info_skl_gt3 = { .gen = 9 };
49static const struct intel_device_info device_info_skl_gt4 = { .gen = 9 };
50static const struct intel_device_info device_info_bxt = { .gen = 9 };
51static const struct intel_device_info device_info_bxt_2x6 = { .gen = 9 };
52static const struct intel_device_info device_info_kbl_gt1 = { .gen = 9 };
53static const struct intel_device_info device_info_kbl_gt1_5 = { .gen = 9 };
54static const struct intel_device_info device_info_kbl_gt2 = { .gen = 9 };
55static const struct intel_device_info device_info_kbl_gt3 = { .gen = 9 };
56static const struct intel_device_info device_info_kbl_gt4 = { .gen = 9 };
57static const struct intel_device_info device_info_glk = { .gen = 9 };
58static const struct intel_device_info device_info_glk_2x6 = { .gen = 9 };
59static const struct intel_device_info device_info_cfl_gt1 = { .gen = 9 };
60static const struct intel_device_info device_info_cfl_gt2 = { .gen = 9 };
61static const struct intel_device_info device_info_cfl_gt3 = { .gen = 9 };
62static const struct intel_device_info device_info_cnl_2x8 = { .gen = 10 };
63static const struct intel_device_info device_info_cnl_3x8 = { .gen = 10 };
64static const struct intel_device_info device_info_cnl_4x8 = { .gen = 10 };
65static const struct intel_device_info device_info_cnl_5x8 = { .gen = 10 };
66static const struct intel_device_info device_info_cnl = { .gen = 10 };
67static const struct intel_device_info device_info_icl = { .gen = 11 };
68static const struct intel_device_info device_info_ehl = { .gen = 11 };
69static const struct intel_device_info device_info_jpl = { .gen = 11 };
70static const struct intel_device_info device_info_gen12 = { .gen = 12 };
71
72static const struct intel_device_info *
73device_info(int device_id)
74{
75 switch (device_id) {
76#define CHIPSET(device_id, type, name) \
77 case device_id: \
78 return &device_info_##type;
79#include "i965_pci_ids.h"
80#undef CHIPSET
81 default:
82 return NULL;
83 }
84}
85
86bool
87intel_batch_initialize(struct intel_batch *batch,
88 drm_intel_bufmgr *bufmgr)
89{
90 int device_id = drm_intel_bufmgr_gem_get_devid(bufmgr);
91
92 batch->command_count = 0;
93 batch->device_info = device_info(device_id);
94
95 if (!batch->device_info)
96 return false;
97
98 /* Alignment argument (4096) is not used */
99 batch->bo = drm_intel_bo_alloc(bufmgr, "batchbuffer", sizeof batch->commands, 4096);
100
101 if (!batch->bo)
102 return false;
103
104 return true;
105}
106
107void
108intel_batch_finalize(struct intel_batch *batch)
109{
110 drm_intel_bo_unreference(batch->bo);
111}
112
113void
114intel_batch_flush(struct intel_batch *batch)
115{
116 if (batch->command_count == 0)
117 return;
118
119 intel_batch_add_dword(batch, MI_BATCH_BUFFER_END);
120
121 /* Pad the batch buffer to the next quad-word. */
122 if (batch->command_count & 1)
123 intel_batch_add_dword(batch, MI_NOOP);
124
125 drm_intel_bo_subdata(batch->bo, 0, batch->command_count << 2,
126 batch->commands);
127 drm_intel_bo_mrb_exec(batch->bo, batch->command_count << 2, NULL, 0, 0,
128 GEN(batch, 6) ? I915_EXEC_BLT
129 : I915_EXEC_DEFAULT);
130 drm_intel_gem_bo_clear_relocs(batch->bo, 0);
131 batch->command_count = 0;
132}