i915_gem.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678
  1. /*
  2. * Copyright © 2008 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
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/i915_drm.h>
  29. #include "i915_drv.h"
  30. #include "i915_trace.h"
  31. #include "intel_drv.h"
  32. #include <linux/shmem_fs.h>
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-buf.h>
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  39. static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  40. unsigned alignment,
  41. bool map_and_fenceable,
  42. bool nonblocking);
  43. static int i915_gem_phys_pwrite(struct drm_device *dev,
  44. struct drm_i915_gem_object *obj,
  45. struct drm_i915_gem_pwrite *args,
  46. struct drm_file *file);
  47. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  48. struct drm_i915_gem_object *obj);
  49. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  50. struct drm_i915_fence_reg *fence,
  51. bool enable);
  52. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  53. struct shrink_control *sc);
  54. static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  55. static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  56. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  57. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  58. {
  59. if (obj->tiling_mode)
  60. i915_gem_release_mmap(obj);
  61. /* As we do not have an associated fence register, we will force
  62. * a tiling change if we ever need to acquire one.
  63. */
  64. obj->fence_dirty = false;
  65. obj->fence_reg = I915_FENCE_REG_NONE;
  66. }
  67. /* some bookkeeping */
  68. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  69. size_t size)
  70. {
  71. dev_priv->mm.object_count++;
  72. dev_priv->mm.object_memory += size;
  73. }
  74. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  75. size_t size)
  76. {
  77. dev_priv->mm.object_count--;
  78. dev_priv->mm.object_memory -= size;
  79. }
  80. static int
  81. i915_gem_wait_for_error(struct i915_gpu_error *error)
  82. {
  83. int ret;
  84. #define EXIT_COND (!i915_reset_in_progress(error) || \
  85. i915_terminally_wedged(error))
  86. if (EXIT_COND)
  87. return 0;
  88. /*
  89. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  90. * userspace. If it takes that long something really bad is going on and
  91. * we should simply try to bail out and fail as gracefully as possible.
  92. */
  93. ret = wait_event_interruptible_timeout(error->reset_queue,
  94. EXIT_COND,
  95. 10*HZ);
  96. if (ret == 0) {
  97. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  98. return -EIO;
  99. } else if (ret < 0) {
  100. return ret;
  101. }
  102. #undef EXIT_COND
  103. return 0;
  104. }
  105. int i915_mutex_lock_interruptible(struct drm_device *dev)
  106. {
  107. struct drm_i915_private *dev_priv = dev->dev_private;
  108. int ret;
  109. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  110. if (ret)
  111. return ret;
  112. ret = mutex_lock_interruptible(&dev->struct_mutex);
  113. if (ret)
  114. return ret;
  115. WARN_ON(i915_verify_lists(dev));
  116. return 0;
  117. }
  118. static inline bool
  119. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  120. {
  121. return i915_gem_obj_ggtt_bound(obj) && !obj->active;
  122. }
  123. int
  124. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  125. struct drm_file *file)
  126. {
  127. struct drm_i915_private *dev_priv = dev->dev_private;
  128. struct drm_i915_gem_init *args = data;
  129. if (drm_core_check_feature(dev, DRIVER_MODESET))
  130. return -ENODEV;
  131. if (args->gtt_start >= args->gtt_end ||
  132. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  133. return -EINVAL;
  134. /* GEM with user mode setting was never supported on ilk and later. */
  135. if (INTEL_INFO(dev)->gen >= 5)
  136. return -ENODEV;
  137. mutex_lock(&dev->struct_mutex);
  138. i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
  139. args->gtt_end);
  140. dev_priv->gtt.mappable_end = args->gtt_end;
  141. mutex_unlock(&dev->struct_mutex);
  142. return 0;
  143. }
  144. int
  145. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  146. struct drm_file *file)
  147. {
  148. struct drm_i915_private *dev_priv = dev->dev_private;
  149. struct drm_i915_gem_get_aperture *args = data;
  150. struct drm_i915_gem_object *obj;
  151. size_t pinned;
  152. pinned = 0;
  153. mutex_lock(&dev->struct_mutex);
  154. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  155. if (obj->pin_count)
  156. pinned += i915_gem_obj_ggtt_size(obj);
  157. mutex_unlock(&dev->struct_mutex);
  158. args->aper_size = dev_priv->gtt.base.total;
  159. args->aper_available_size = args->aper_size - pinned;
  160. return 0;
  161. }
  162. void *i915_gem_object_alloc(struct drm_device *dev)
  163. {
  164. struct drm_i915_private *dev_priv = dev->dev_private;
  165. return kmem_cache_alloc(dev_priv->slab, GFP_KERNEL | __GFP_ZERO);
  166. }
  167. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  168. {
  169. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  170. kmem_cache_free(dev_priv->slab, obj);
  171. }
  172. static int
  173. i915_gem_create(struct drm_file *file,
  174. struct drm_device *dev,
  175. uint64_t size,
  176. uint32_t *handle_p)
  177. {
  178. struct drm_i915_gem_object *obj;
  179. int ret;
  180. u32 handle;
  181. size = roundup(size, PAGE_SIZE);
  182. if (size == 0)
  183. return -EINVAL;
  184. /* Allocate the new object */
  185. obj = i915_gem_alloc_object(dev, size);
  186. if (obj == NULL)
  187. return -ENOMEM;
  188. ret = drm_gem_handle_create(file, &obj->base, &handle);
  189. if (ret) {
  190. drm_gem_object_release(&obj->base);
  191. i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
  192. i915_gem_object_free(obj);
  193. return ret;
  194. }
  195. /* drop reference from allocate - handle holds it now */
  196. drm_gem_object_unreference(&obj->base);
  197. trace_i915_gem_object_create(obj);
  198. *handle_p = handle;
  199. return 0;
  200. }
  201. int
  202. i915_gem_dumb_create(struct drm_file *file,
  203. struct drm_device *dev,
  204. struct drm_mode_create_dumb *args)
  205. {
  206. /* have to work out size/pitch and return them */
  207. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  208. args->size = args->pitch * args->height;
  209. return i915_gem_create(file, dev,
  210. args->size, &args->handle);
  211. }
  212. int i915_gem_dumb_destroy(struct drm_file *file,
  213. struct drm_device *dev,
  214. uint32_t handle)
  215. {
  216. return drm_gem_handle_delete(file, handle);
  217. }
  218. /**
  219. * Creates a new mm object and returns a handle to it.
  220. */
  221. int
  222. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  223. struct drm_file *file)
  224. {
  225. struct drm_i915_gem_create *args = data;
  226. return i915_gem_create(file, dev,
  227. args->size, &args->handle);
  228. }
  229. static inline int
  230. __copy_to_user_swizzled(char __user *cpu_vaddr,
  231. const char *gpu_vaddr, int gpu_offset,
  232. int length)
  233. {
  234. int ret, cpu_offset = 0;
  235. while (length > 0) {
  236. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  237. int this_length = min(cacheline_end - gpu_offset, length);
  238. int swizzled_gpu_offset = gpu_offset ^ 64;
  239. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  240. gpu_vaddr + swizzled_gpu_offset,
  241. this_length);
  242. if (ret)
  243. return ret + length;
  244. cpu_offset += this_length;
  245. gpu_offset += this_length;
  246. length -= this_length;
  247. }
  248. return 0;
  249. }
  250. static inline int
  251. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  252. const char __user *cpu_vaddr,
  253. int length)
  254. {
  255. int ret, cpu_offset = 0;
  256. while (length > 0) {
  257. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  258. int this_length = min(cacheline_end - gpu_offset, length);
  259. int swizzled_gpu_offset = gpu_offset ^ 64;
  260. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  261. cpu_vaddr + cpu_offset,
  262. this_length);
  263. if (ret)
  264. return ret + length;
  265. cpu_offset += this_length;
  266. gpu_offset += this_length;
  267. length -= this_length;
  268. }
  269. return 0;
  270. }
  271. /* Per-page copy function for the shmem pread fastpath.
  272. * Flushes invalid cachelines before reading the target if
  273. * needs_clflush is set. */
  274. static int
  275. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  276. char __user *user_data,
  277. bool page_do_bit17_swizzling, bool needs_clflush)
  278. {
  279. char *vaddr;
  280. int ret;
  281. if (unlikely(page_do_bit17_swizzling))
  282. return -EINVAL;
  283. vaddr = kmap_atomic(page);
  284. if (needs_clflush)
  285. drm_clflush_virt_range(vaddr + shmem_page_offset,
  286. page_length);
  287. ret = __copy_to_user_inatomic(user_data,
  288. vaddr + shmem_page_offset,
  289. page_length);
  290. kunmap_atomic(vaddr);
  291. return ret ? -EFAULT : 0;
  292. }
  293. static void
  294. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  295. bool swizzled)
  296. {
  297. if (unlikely(swizzled)) {
  298. unsigned long start = (unsigned long) addr;
  299. unsigned long end = (unsigned long) addr + length;
  300. /* For swizzling simply ensure that we always flush both
  301. * channels. Lame, but simple and it works. Swizzled
  302. * pwrite/pread is far from a hotpath - current userspace
  303. * doesn't use it at all. */
  304. start = round_down(start, 128);
  305. end = round_up(end, 128);
  306. drm_clflush_virt_range((void *)start, end - start);
  307. } else {
  308. drm_clflush_virt_range(addr, length);
  309. }
  310. }
  311. /* Only difference to the fast-path function is that this can handle bit17
  312. * and uses non-atomic copy and kmap functions. */
  313. static int
  314. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  315. char __user *user_data,
  316. bool page_do_bit17_swizzling, bool needs_clflush)
  317. {
  318. char *vaddr;
  319. int ret;
  320. vaddr = kmap(page);
  321. if (needs_clflush)
  322. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  323. page_length,
  324. page_do_bit17_swizzling);
  325. if (page_do_bit17_swizzling)
  326. ret = __copy_to_user_swizzled(user_data,
  327. vaddr, shmem_page_offset,
  328. page_length);
  329. else
  330. ret = __copy_to_user(user_data,
  331. vaddr + shmem_page_offset,
  332. page_length);
  333. kunmap(page);
  334. return ret ? - EFAULT : 0;
  335. }
  336. static int
  337. i915_gem_shmem_pread(struct drm_device *dev,
  338. struct drm_i915_gem_object *obj,
  339. struct drm_i915_gem_pread *args,
  340. struct drm_file *file)
  341. {
  342. char __user *user_data;
  343. ssize_t remain;
  344. loff_t offset;
  345. int shmem_page_offset, page_length, ret = 0;
  346. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  347. int prefaulted = 0;
  348. int needs_clflush = 0;
  349. struct sg_page_iter sg_iter;
  350. user_data = to_user_ptr(args->data_ptr);
  351. remain = args->size;
  352. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  353. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  354. /* If we're not in the cpu read domain, set ourself into the gtt
  355. * read domain and manually flush cachelines (if required). This
  356. * optimizes for the case when the gpu will dirty the data
  357. * anyway again before the next pread happens. */
  358. if (obj->cache_level == I915_CACHE_NONE)
  359. needs_clflush = 1;
  360. if (i915_gem_obj_ggtt_bound(obj)) {
  361. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  362. if (ret)
  363. return ret;
  364. }
  365. }
  366. ret = i915_gem_object_get_pages(obj);
  367. if (ret)
  368. return ret;
  369. i915_gem_object_pin_pages(obj);
  370. offset = args->offset;
  371. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  372. offset >> PAGE_SHIFT) {
  373. struct page *page = sg_page_iter_page(&sg_iter);
  374. if (remain <= 0)
  375. break;
  376. /* Operation in this page
  377. *
  378. * shmem_page_offset = offset within page in shmem file
  379. * page_length = bytes to copy for this page
  380. */
  381. shmem_page_offset = offset_in_page(offset);
  382. page_length = remain;
  383. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  384. page_length = PAGE_SIZE - shmem_page_offset;
  385. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  386. (page_to_phys(page) & (1 << 17)) != 0;
  387. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  388. user_data, page_do_bit17_swizzling,
  389. needs_clflush);
  390. if (ret == 0)
  391. goto next_page;
  392. mutex_unlock(&dev->struct_mutex);
  393. if (likely(!i915_prefault_disable) && !prefaulted) {
  394. ret = fault_in_multipages_writeable(user_data, remain);
  395. /* Userspace is tricking us, but we've already clobbered
  396. * its pages with the prefault and promised to write the
  397. * data up to the first fault. Hence ignore any errors
  398. * and just continue. */
  399. (void)ret;
  400. prefaulted = 1;
  401. }
  402. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  403. user_data, page_do_bit17_swizzling,
  404. needs_clflush);
  405. mutex_lock(&dev->struct_mutex);
  406. next_page:
  407. mark_page_accessed(page);
  408. if (ret)
  409. goto out;
  410. remain -= page_length;
  411. user_data += page_length;
  412. offset += page_length;
  413. }
  414. out:
  415. i915_gem_object_unpin_pages(obj);
  416. return ret;
  417. }
  418. /**
  419. * Reads data from the object referenced by handle.
  420. *
  421. * On error, the contents of *data are undefined.
  422. */
  423. int
  424. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  425. struct drm_file *file)
  426. {
  427. struct drm_i915_gem_pread *args = data;
  428. struct drm_i915_gem_object *obj;
  429. int ret = 0;
  430. if (args->size == 0)
  431. return 0;
  432. if (!access_ok(VERIFY_WRITE,
  433. to_user_ptr(args->data_ptr),
  434. args->size))
  435. return -EFAULT;
  436. ret = i915_mutex_lock_interruptible(dev);
  437. if (ret)
  438. return ret;
  439. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  440. if (&obj->base == NULL) {
  441. ret = -ENOENT;
  442. goto unlock;
  443. }
  444. /* Bounds check source. */
  445. if (args->offset > obj->base.size ||
  446. args->size > obj->base.size - args->offset) {
  447. ret = -EINVAL;
  448. goto out;
  449. }
  450. /* prime objects have no backing filp to GEM pread/pwrite
  451. * pages from.
  452. */
  453. if (!obj->base.filp) {
  454. ret = -EINVAL;
  455. goto out;
  456. }
  457. trace_i915_gem_object_pread(obj, args->offset, args->size);
  458. ret = i915_gem_shmem_pread(dev, obj, args, file);
  459. out:
  460. drm_gem_object_unreference(&obj->base);
  461. unlock:
  462. mutex_unlock(&dev->struct_mutex);
  463. return ret;
  464. }
  465. /* This is the fast write path which cannot handle
  466. * page faults in the source data
  467. */
  468. static inline int
  469. fast_user_write(struct io_mapping *mapping,
  470. loff_t page_base, int page_offset,
  471. char __user *user_data,
  472. int length)
  473. {
  474. void __iomem *vaddr_atomic;
  475. void *vaddr;
  476. unsigned long unwritten;
  477. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  478. /* We can use the cpu mem copy function because this is X86. */
  479. vaddr = (void __force*)vaddr_atomic + page_offset;
  480. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  481. user_data, length);
  482. io_mapping_unmap_atomic(vaddr_atomic);
  483. return unwritten;
  484. }
  485. /**
  486. * This is the fast pwrite path, where we copy the data directly from the
  487. * user into the GTT, uncached.
  488. */
  489. static int
  490. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  491. struct drm_i915_gem_object *obj,
  492. struct drm_i915_gem_pwrite *args,
  493. struct drm_file *file)
  494. {
  495. drm_i915_private_t *dev_priv = dev->dev_private;
  496. ssize_t remain;
  497. loff_t offset, page_base;
  498. char __user *user_data;
  499. int page_offset, page_length, ret;
  500. ret = i915_gem_object_pin(obj, 0, true, true);
  501. if (ret)
  502. goto out;
  503. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  504. if (ret)
  505. goto out_unpin;
  506. ret = i915_gem_object_put_fence(obj);
  507. if (ret)
  508. goto out_unpin;
  509. user_data = to_user_ptr(args->data_ptr);
  510. remain = args->size;
  511. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  512. while (remain > 0) {
  513. /* Operation in this page
  514. *
  515. * page_base = page offset within aperture
  516. * page_offset = offset within page
  517. * page_length = bytes to copy for this page
  518. */
  519. page_base = offset & PAGE_MASK;
  520. page_offset = offset_in_page(offset);
  521. page_length = remain;
  522. if ((page_offset + remain) > PAGE_SIZE)
  523. page_length = PAGE_SIZE - page_offset;
  524. /* If we get a fault while copying data, then (presumably) our
  525. * source page isn't available. Return the error and we'll
  526. * retry in the slow path.
  527. */
  528. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  529. page_offset, user_data, page_length)) {
  530. ret = -EFAULT;
  531. goto out_unpin;
  532. }
  533. remain -= page_length;
  534. user_data += page_length;
  535. offset += page_length;
  536. }
  537. out_unpin:
  538. i915_gem_object_unpin(obj);
  539. out:
  540. return ret;
  541. }
  542. /* Per-page copy function for the shmem pwrite fastpath.
  543. * Flushes invalid cachelines before writing to the target if
  544. * needs_clflush_before is set and flushes out any written cachelines after
  545. * writing if needs_clflush is set. */
  546. static int
  547. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  548. char __user *user_data,
  549. bool page_do_bit17_swizzling,
  550. bool needs_clflush_before,
  551. bool needs_clflush_after)
  552. {
  553. char *vaddr;
  554. int ret;
  555. if (unlikely(page_do_bit17_swizzling))
  556. return -EINVAL;
  557. vaddr = kmap_atomic(page);
  558. if (needs_clflush_before)
  559. drm_clflush_virt_range(vaddr + shmem_page_offset,
  560. page_length);
  561. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  562. user_data,
  563. page_length);
  564. if (needs_clflush_after)
  565. drm_clflush_virt_range(vaddr + shmem_page_offset,
  566. page_length);
  567. kunmap_atomic(vaddr);
  568. return ret ? -EFAULT : 0;
  569. }
  570. /* Only difference to the fast-path function is that this can handle bit17
  571. * and uses non-atomic copy and kmap functions. */
  572. static int
  573. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  574. char __user *user_data,
  575. bool page_do_bit17_swizzling,
  576. bool needs_clflush_before,
  577. bool needs_clflush_after)
  578. {
  579. char *vaddr;
  580. int ret;
  581. vaddr = kmap(page);
  582. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  583. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  584. page_length,
  585. page_do_bit17_swizzling);
  586. if (page_do_bit17_swizzling)
  587. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  588. user_data,
  589. page_length);
  590. else
  591. ret = __copy_from_user(vaddr + shmem_page_offset,
  592. user_data,
  593. page_length);
  594. if (needs_clflush_after)
  595. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  596. page_length,
  597. page_do_bit17_swizzling);
  598. kunmap(page);
  599. return ret ? -EFAULT : 0;
  600. }
  601. static int
  602. i915_gem_shmem_pwrite(struct drm_device *dev,
  603. struct drm_i915_gem_object *obj,
  604. struct drm_i915_gem_pwrite *args,
  605. struct drm_file *file)
  606. {
  607. ssize_t remain;
  608. loff_t offset;
  609. char __user *user_data;
  610. int shmem_page_offset, page_length, ret = 0;
  611. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  612. int hit_slowpath = 0;
  613. int needs_clflush_after = 0;
  614. int needs_clflush_before = 0;
  615. struct sg_page_iter sg_iter;
  616. user_data = to_user_ptr(args->data_ptr);
  617. remain = args->size;
  618. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  619. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  620. /* If we're not in the cpu write domain, set ourself into the gtt
  621. * write domain and manually flush cachelines (if required). This
  622. * optimizes for the case when the gpu will use the data
  623. * right away and we therefore have to clflush anyway. */
  624. if (obj->cache_level == I915_CACHE_NONE)
  625. needs_clflush_after = 1;
  626. if (i915_gem_obj_ggtt_bound(obj)) {
  627. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  628. if (ret)
  629. return ret;
  630. }
  631. }
  632. /* Same trick applies for invalidate partially written cachelines before
  633. * writing. */
  634. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
  635. && obj->cache_level == I915_CACHE_NONE)
  636. needs_clflush_before = 1;
  637. ret = i915_gem_object_get_pages(obj);
  638. if (ret)
  639. return ret;
  640. i915_gem_object_pin_pages(obj);
  641. offset = args->offset;
  642. obj->dirty = 1;
  643. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  644. offset >> PAGE_SHIFT) {
  645. struct page *page = sg_page_iter_page(&sg_iter);
  646. int partial_cacheline_write;
  647. if (remain <= 0)
  648. break;
  649. /* Operation in this page
  650. *
  651. * shmem_page_offset = offset within page in shmem file
  652. * page_length = bytes to copy for this page
  653. */
  654. shmem_page_offset = offset_in_page(offset);
  655. page_length = remain;
  656. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  657. page_length = PAGE_SIZE - shmem_page_offset;
  658. /* If we don't overwrite a cacheline completely we need to be
  659. * careful to have up-to-date data by first clflushing. Don't
  660. * overcomplicate things and flush the entire patch. */
  661. partial_cacheline_write = needs_clflush_before &&
  662. ((shmem_page_offset | page_length)
  663. & (boot_cpu_data.x86_clflush_size - 1));
  664. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  665. (page_to_phys(page) & (1 << 17)) != 0;
  666. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  667. user_data, page_do_bit17_swizzling,
  668. partial_cacheline_write,
  669. needs_clflush_after);
  670. if (ret == 0)
  671. goto next_page;
  672. hit_slowpath = 1;
  673. mutex_unlock(&dev->struct_mutex);
  674. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  675. user_data, page_do_bit17_swizzling,
  676. partial_cacheline_write,
  677. needs_clflush_after);
  678. mutex_lock(&dev->struct_mutex);
  679. next_page:
  680. set_page_dirty(page);
  681. mark_page_accessed(page);
  682. if (ret)
  683. goto out;
  684. remain -= page_length;
  685. user_data += page_length;
  686. offset += page_length;
  687. }
  688. out:
  689. i915_gem_object_unpin_pages(obj);
  690. if (hit_slowpath) {
  691. /*
  692. * Fixup: Flush cpu caches in case we didn't flush the dirty
  693. * cachelines in-line while writing and the object moved
  694. * out of the cpu write domain while we've dropped the lock.
  695. */
  696. if (!needs_clflush_after &&
  697. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  698. i915_gem_clflush_object(obj);
  699. i915_gem_chipset_flush(dev);
  700. }
  701. }
  702. if (needs_clflush_after)
  703. i915_gem_chipset_flush(dev);
  704. return ret;
  705. }
  706. /**
  707. * Writes data to the object referenced by handle.
  708. *
  709. * On error, the contents of the buffer that were to be modified are undefined.
  710. */
  711. int
  712. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  713. struct drm_file *file)
  714. {
  715. struct drm_i915_gem_pwrite *args = data;
  716. struct drm_i915_gem_object *obj;
  717. int ret;
  718. if (args->size == 0)
  719. return 0;
  720. if (!access_ok(VERIFY_READ,
  721. to_user_ptr(args->data_ptr),
  722. args->size))
  723. return -EFAULT;
  724. if (likely(!i915_prefault_disable)) {
  725. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  726. args->size);
  727. if (ret)
  728. return -EFAULT;
  729. }
  730. ret = i915_mutex_lock_interruptible(dev);
  731. if (ret)
  732. return ret;
  733. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  734. if (&obj->base == NULL) {
  735. ret = -ENOENT;
  736. goto unlock;
  737. }
  738. /* Bounds check destination. */
  739. if (args->offset > obj->base.size ||
  740. args->size > obj->base.size - args->offset) {
  741. ret = -EINVAL;
  742. goto out;
  743. }
  744. /* prime objects have no backing filp to GEM pread/pwrite
  745. * pages from.
  746. */
  747. if (!obj->base.filp) {
  748. ret = -EINVAL;
  749. goto out;
  750. }
  751. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  752. ret = -EFAULT;
  753. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  754. * it would end up going through the fenced access, and we'll get
  755. * different detiling behavior between reading and writing.
  756. * pread/pwrite currently are reading and writing from the CPU
  757. * perspective, requiring manual detiling by the client.
  758. */
  759. if (obj->phys_obj) {
  760. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  761. goto out;
  762. }
  763. if (obj->cache_level == I915_CACHE_NONE &&
  764. obj->tiling_mode == I915_TILING_NONE &&
  765. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  766. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  767. /* Note that the gtt paths might fail with non-page-backed user
  768. * pointers (e.g. gtt mappings when moving data between
  769. * textures). Fallback to the shmem path in that case. */
  770. }
  771. if (ret == -EFAULT || ret == -ENOSPC)
  772. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  773. out:
  774. drm_gem_object_unreference(&obj->base);
  775. unlock:
  776. mutex_unlock(&dev->struct_mutex);
  777. return ret;
  778. }
  779. int
  780. i915_gem_check_wedge(struct i915_gpu_error *error,
  781. bool interruptible)
  782. {
  783. if (i915_reset_in_progress(error)) {
  784. /* Non-interruptible callers can't handle -EAGAIN, hence return
  785. * -EIO unconditionally for these. */
  786. if (!interruptible)
  787. return -EIO;
  788. /* Recovery complete, but the reset failed ... */
  789. if (i915_terminally_wedged(error))
  790. return -EIO;
  791. return -EAGAIN;
  792. }
  793. return 0;
  794. }
  795. /*
  796. * Compare seqno against outstanding lazy request. Emit a request if they are
  797. * equal.
  798. */
  799. static int
  800. i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
  801. {
  802. int ret;
  803. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  804. ret = 0;
  805. if (seqno == ring->outstanding_lazy_request)
  806. ret = i915_add_request(ring, NULL);
  807. return ret;
  808. }
  809. /**
  810. * __wait_seqno - wait until execution of seqno has finished
  811. * @ring: the ring expected to report seqno
  812. * @seqno: duh!
  813. * @reset_counter: reset sequence associated with the given seqno
  814. * @interruptible: do an interruptible wait (normally yes)
  815. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  816. *
  817. * Note: It is of utmost importance that the passed in seqno and reset_counter
  818. * values have been read by the caller in an smp safe manner. Where read-side
  819. * locks are involved, it is sufficient to read the reset_counter before
  820. * unlocking the lock that protects the seqno. For lockless tricks, the
  821. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  822. * inserted.
  823. *
  824. * Returns 0 if the seqno was found within the alloted time. Else returns the
  825. * errno with remaining time filled in timeout argument.
  826. */
  827. static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
  828. unsigned reset_counter,
  829. bool interruptible, struct timespec *timeout)
  830. {
  831. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  832. struct timespec before, now, wait_time={1,0};
  833. unsigned long timeout_jiffies;
  834. long end;
  835. bool wait_forever = true;
  836. int ret;
  837. if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  838. return 0;
  839. trace_i915_gem_request_wait_begin(ring, seqno);
  840. if (timeout != NULL) {
  841. wait_time = *timeout;
  842. wait_forever = false;
  843. }
  844. timeout_jiffies = timespec_to_jiffies_timeout(&wait_time);
  845. if (WARN_ON(!ring->irq_get(ring)))
  846. return -ENODEV;
  847. /* Record current time in case interrupted by signal, or wedged * */
  848. getrawmonotonic(&before);
  849. #define EXIT_COND \
  850. (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
  851. i915_reset_in_progress(&dev_priv->gpu_error) || \
  852. reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  853. do {
  854. if (interruptible)
  855. end = wait_event_interruptible_timeout(ring->irq_queue,
  856. EXIT_COND,
  857. timeout_jiffies);
  858. else
  859. end = wait_event_timeout(ring->irq_queue, EXIT_COND,
  860. timeout_jiffies);
  861. /* We need to check whether any gpu reset happened in between
  862. * the caller grabbing the seqno and now ... */
  863. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  864. end = -EAGAIN;
  865. /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
  866. * gone. */
  867. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  868. if (ret)
  869. end = ret;
  870. } while (end == 0 && wait_forever);
  871. getrawmonotonic(&now);
  872. ring->irq_put(ring);
  873. trace_i915_gem_request_wait_end(ring, seqno);
  874. #undef EXIT_COND
  875. if (timeout) {
  876. struct timespec sleep_time = timespec_sub(now, before);
  877. *timeout = timespec_sub(*timeout, sleep_time);
  878. if (!timespec_valid(timeout)) /* i.e. negative time remains */
  879. set_normalized_timespec(timeout, 0, 0);
  880. }
  881. switch (end) {
  882. case -EIO:
  883. case -EAGAIN: /* Wedged */
  884. case -ERESTARTSYS: /* Signal */
  885. return (int)end;
  886. case 0: /* Timeout */
  887. return -ETIME;
  888. default: /* Completed */
  889. WARN_ON(end < 0); /* We're not aware of other errors */
  890. return 0;
  891. }
  892. }
  893. /**
  894. * Waits for a sequence number to be signaled, and cleans up the
  895. * request and object lists appropriately for that event.
  896. */
  897. int
  898. i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
  899. {
  900. struct drm_device *dev = ring->dev;
  901. struct drm_i915_private *dev_priv = dev->dev_private;
  902. bool interruptible = dev_priv->mm.interruptible;
  903. int ret;
  904. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  905. BUG_ON(seqno == 0);
  906. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  907. if (ret)
  908. return ret;
  909. ret = i915_gem_check_olr(ring, seqno);
  910. if (ret)
  911. return ret;
  912. return __wait_seqno(ring, seqno,
  913. atomic_read(&dev_priv->gpu_error.reset_counter),
  914. interruptible, NULL);
  915. }
  916. static int
  917. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
  918. struct intel_ring_buffer *ring)
  919. {
  920. i915_gem_retire_requests_ring(ring);
  921. /* Manually manage the write flush as we may have not yet
  922. * retired the buffer.
  923. *
  924. * Note that the last_write_seqno is always the earlier of
  925. * the two (read/write) seqno, so if we haved successfully waited,
  926. * we know we have passed the last write.
  927. */
  928. obj->last_write_seqno = 0;
  929. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  930. return 0;
  931. }
  932. /**
  933. * Ensures that all rendering to the object has completed and the object is
  934. * safe to unbind from the GTT or access from the CPU.
  935. */
  936. static __must_check int
  937. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  938. bool readonly)
  939. {
  940. struct intel_ring_buffer *ring = obj->ring;
  941. u32 seqno;
  942. int ret;
  943. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  944. if (seqno == 0)
  945. return 0;
  946. ret = i915_wait_seqno(ring, seqno);
  947. if (ret)
  948. return ret;
  949. return i915_gem_object_wait_rendering__tail(obj, ring);
  950. }
  951. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  952. * as the object state may change during this call.
  953. */
  954. static __must_check int
  955. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  956. bool readonly)
  957. {
  958. struct drm_device *dev = obj->base.dev;
  959. struct drm_i915_private *dev_priv = dev->dev_private;
  960. struct intel_ring_buffer *ring = obj->ring;
  961. unsigned reset_counter;
  962. u32 seqno;
  963. int ret;
  964. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  965. BUG_ON(!dev_priv->mm.interruptible);
  966. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  967. if (seqno == 0)
  968. return 0;
  969. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  970. if (ret)
  971. return ret;
  972. ret = i915_gem_check_olr(ring, seqno);
  973. if (ret)
  974. return ret;
  975. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  976. mutex_unlock(&dev->struct_mutex);
  977. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  978. mutex_lock(&dev->struct_mutex);
  979. if (ret)
  980. return ret;
  981. return i915_gem_object_wait_rendering__tail(obj, ring);
  982. }
  983. /**
  984. * Called when user space prepares to use an object with the CPU, either
  985. * through the mmap ioctl's mapping or a GTT mapping.
  986. */
  987. int
  988. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  989. struct drm_file *file)
  990. {
  991. struct drm_i915_gem_set_domain *args = data;
  992. struct drm_i915_gem_object *obj;
  993. uint32_t read_domains = args->read_domains;
  994. uint32_t write_domain = args->write_domain;
  995. int ret;
  996. /* Only handle setting domains to types used by the CPU. */
  997. if (write_domain & I915_GEM_GPU_DOMAINS)
  998. return -EINVAL;
  999. if (read_domains & I915_GEM_GPU_DOMAINS)
  1000. return -EINVAL;
  1001. /* Having something in the write domain implies it's in the read
  1002. * domain, and only that read domain. Enforce that in the request.
  1003. */
  1004. if (write_domain != 0 && read_domains != write_domain)
  1005. return -EINVAL;
  1006. ret = i915_mutex_lock_interruptible(dev);
  1007. if (ret)
  1008. return ret;
  1009. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1010. if (&obj->base == NULL) {
  1011. ret = -ENOENT;
  1012. goto unlock;
  1013. }
  1014. /* Try to flush the object off the GPU without holding the lock.
  1015. * We will repeat the flush holding the lock in the normal manner
  1016. * to catch cases where we are gazumped.
  1017. */
  1018. ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
  1019. if (ret)
  1020. goto unref;
  1021. if (read_domains & I915_GEM_DOMAIN_GTT) {
  1022. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1023. /* Silently promote "you're not bound, there was nothing to do"
  1024. * to success, since the client was just asking us to
  1025. * make sure everything was done.
  1026. */
  1027. if (ret == -EINVAL)
  1028. ret = 0;
  1029. } else {
  1030. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1031. }
  1032. unref:
  1033. drm_gem_object_unreference(&obj->base);
  1034. unlock:
  1035. mutex_unlock(&dev->struct_mutex);
  1036. return ret;
  1037. }
  1038. /**
  1039. * Called when user space has done writes to this buffer
  1040. */
  1041. int
  1042. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1043. struct drm_file *file)
  1044. {
  1045. struct drm_i915_gem_sw_finish *args = data;
  1046. struct drm_i915_gem_object *obj;
  1047. int ret = 0;
  1048. ret = i915_mutex_lock_interruptible(dev);
  1049. if (ret)
  1050. return ret;
  1051. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1052. if (&obj->base == NULL) {
  1053. ret = -ENOENT;
  1054. goto unlock;
  1055. }
  1056. /* Pinned buffers may be scanout, so flush the cache */
  1057. if (obj->pin_count)
  1058. i915_gem_object_flush_cpu_write_domain(obj);
  1059. drm_gem_object_unreference(&obj->base);
  1060. unlock:
  1061. mutex_unlock(&dev->struct_mutex);
  1062. return ret;
  1063. }
  1064. /**
  1065. * Maps the contents of an object, returning the address it is mapped
  1066. * into.
  1067. *
  1068. * While the mapping holds a reference on the contents of the object, it doesn't
  1069. * imply a ref on the object itself.
  1070. */
  1071. int
  1072. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1073. struct drm_file *file)
  1074. {
  1075. struct drm_i915_gem_mmap *args = data;
  1076. struct drm_gem_object *obj;
  1077. unsigned long addr;
  1078. obj = drm_gem_object_lookup(dev, file, args->handle);
  1079. if (obj == NULL)
  1080. return -ENOENT;
  1081. /* prime objects have no backing filp to GEM mmap
  1082. * pages from.
  1083. */
  1084. if (!obj->filp) {
  1085. drm_gem_object_unreference_unlocked(obj);
  1086. return -EINVAL;
  1087. }
  1088. addr = vm_mmap(obj->filp, 0, args->size,
  1089. PROT_READ | PROT_WRITE, MAP_SHARED,
  1090. args->offset);
  1091. drm_gem_object_unreference_unlocked(obj);
  1092. if (IS_ERR((void *)addr))
  1093. return addr;
  1094. args->addr_ptr = (uint64_t) addr;
  1095. return 0;
  1096. }
  1097. /**
  1098. * i915_gem_fault - fault a page into the GTT
  1099. * vma: VMA in question
  1100. * vmf: fault info
  1101. *
  1102. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1103. * from userspace. The fault handler takes care of binding the object to
  1104. * the GTT (if needed), allocating and programming a fence register (again,
  1105. * only if needed based on whether the old reg is still valid or the object
  1106. * is tiled) and inserting a new PTE into the faulting process.
  1107. *
  1108. * Note that the faulting process may involve evicting existing objects
  1109. * from the GTT and/or fence registers to make room. So performance may
  1110. * suffer if the GTT working set is large or there are few fence registers
  1111. * left.
  1112. */
  1113. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1114. {
  1115. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1116. struct drm_device *dev = obj->base.dev;
  1117. drm_i915_private_t *dev_priv = dev->dev_private;
  1118. pgoff_t page_offset;
  1119. unsigned long pfn;
  1120. int ret = 0;
  1121. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1122. /* We don't use vmf->pgoff since that has the fake offset */
  1123. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1124. PAGE_SHIFT;
  1125. ret = i915_mutex_lock_interruptible(dev);
  1126. if (ret)
  1127. goto out;
  1128. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1129. /* Access to snoopable pages through the GTT is incoherent. */
  1130. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1131. ret = -EINVAL;
  1132. goto unlock;
  1133. }
  1134. /* Now bind it into the GTT if needed */
  1135. ret = i915_gem_object_pin(obj, 0, true, false);
  1136. if (ret)
  1137. goto unlock;
  1138. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1139. if (ret)
  1140. goto unpin;
  1141. ret = i915_gem_object_get_fence(obj);
  1142. if (ret)
  1143. goto unpin;
  1144. obj->fault_mappable = true;
  1145. pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
  1146. pfn >>= PAGE_SHIFT;
  1147. pfn += page_offset;
  1148. /* Finally, remap it using the new GTT offset */
  1149. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1150. unpin:
  1151. i915_gem_object_unpin(obj);
  1152. unlock:
  1153. mutex_unlock(&dev->struct_mutex);
  1154. out:
  1155. switch (ret) {
  1156. case -EIO:
  1157. /* If this -EIO is due to a gpu hang, give the reset code a
  1158. * chance to clean up the mess. Otherwise return the proper
  1159. * SIGBUS. */
  1160. if (i915_terminally_wedged(&dev_priv->gpu_error))
  1161. return VM_FAULT_SIGBUS;
  1162. case -EAGAIN:
  1163. /* Give the error handler a chance to run and move the
  1164. * objects off the GPU active list. Next time we service the
  1165. * fault, we should be able to transition the page into the
  1166. * GTT without touching the GPU (and so avoid further
  1167. * EIO/EGAIN). If the GPU is wedged, then there is no issue
  1168. * with coherency, just lost writes.
  1169. */
  1170. set_need_resched();
  1171. case 0:
  1172. case -ERESTARTSYS:
  1173. case -EINTR:
  1174. case -EBUSY:
  1175. /*
  1176. * EBUSY is ok: this just means that another thread
  1177. * already did the job.
  1178. */
  1179. return VM_FAULT_NOPAGE;
  1180. case -ENOMEM:
  1181. return VM_FAULT_OOM;
  1182. case -ENOSPC:
  1183. return VM_FAULT_SIGBUS;
  1184. default:
  1185. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1186. return VM_FAULT_SIGBUS;
  1187. }
  1188. }
  1189. /**
  1190. * i915_gem_release_mmap - remove physical page mappings
  1191. * @obj: obj in question
  1192. *
  1193. * Preserve the reservation of the mmapping with the DRM core code, but
  1194. * relinquish ownership of the pages back to the system.
  1195. *
  1196. * It is vital that we remove the page mapping if we have mapped a tiled
  1197. * object through the GTT and then lose the fence register due to
  1198. * resource pressure. Similarly if the object has been moved out of the
  1199. * aperture, than pages mapped into userspace must be revoked. Removing the
  1200. * mapping will then trigger a page fault on the next user access, allowing
  1201. * fixup by i915_gem_fault().
  1202. */
  1203. void
  1204. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1205. {
  1206. if (!obj->fault_mappable)
  1207. return;
  1208. if (obj->base.dev->dev_mapping)
  1209. unmap_mapping_range(obj->base.dev->dev_mapping,
  1210. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1211. obj->base.size, 1);
  1212. obj->fault_mappable = false;
  1213. }
  1214. uint32_t
  1215. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1216. {
  1217. uint32_t gtt_size;
  1218. if (INTEL_INFO(dev)->gen >= 4 ||
  1219. tiling_mode == I915_TILING_NONE)
  1220. return size;
  1221. /* Previous chips need a power-of-two fence region when tiling */
  1222. if (INTEL_INFO(dev)->gen == 3)
  1223. gtt_size = 1024*1024;
  1224. else
  1225. gtt_size = 512*1024;
  1226. while (gtt_size < size)
  1227. gtt_size <<= 1;
  1228. return gtt_size;
  1229. }
  1230. /**
  1231. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1232. * @obj: object to check
  1233. *
  1234. * Return the required GTT alignment for an object, taking into account
  1235. * potential fence register mapping.
  1236. */
  1237. uint32_t
  1238. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1239. int tiling_mode, bool fenced)
  1240. {
  1241. /*
  1242. * Minimum alignment is 4k (GTT page size), but might be greater
  1243. * if a fence register is needed for the object.
  1244. */
  1245. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1246. tiling_mode == I915_TILING_NONE)
  1247. return 4096;
  1248. /*
  1249. * Previous chips need to be aligned to the size of the smallest
  1250. * fence register that can contain the object.
  1251. */
  1252. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1253. }
  1254. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1255. {
  1256. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1257. int ret;
  1258. if (obj->base.map_list.map)
  1259. return 0;
  1260. dev_priv->mm.shrinker_no_lock_stealing = true;
  1261. ret = drm_gem_create_mmap_offset(&obj->base);
  1262. if (ret != -ENOSPC)
  1263. goto out;
  1264. /* Badly fragmented mmap space? The only way we can recover
  1265. * space is by destroying unwanted objects. We can't randomly release
  1266. * mmap_offsets as userspace expects them to be persistent for the
  1267. * lifetime of the objects. The closest we can is to release the
  1268. * offsets on purgeable objects by truncating it and marking it purged,
  1269. * which prevents userspace from ever using that object again.
  1270. */
  1271. i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
  1272. ret = drm_gem_create_mmap_offset(&obj->base);
  1273. if (ret != -ENOSPC)
  1274. goto out;
  1275. i915_gem_shrink_all(dev_priv);
  1276. ret = drm_gem_create_mmap_offset(&obj->base);
  1277. out:
  1278. dev_priv->mm.shrinker_no_lock_stealing = false;
  1279. return ret;
  1280. }
  1281. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1282. {
  1283. if (!obj->base.map_list.map)
  1284. return;
  1285. drm_gem_free_mmap_offset(&obj->base);
  1286. }
  1287. int
  1288. i915_gem_mmap_gtt(struct drm_file *file,
  1289. struct drm_device *dev,
  1290. uint32_t handle,
  1291. uint64_t *offset)
  1292. {
  1293. struct drm_i915_private *dev_priv = dev->dev_private;
  1294. struct drm_i915_gem_object *obj;
  1295. int ret;
  1296. ret = i915_mutex_lock_interruptible(dev);
  1297. if (ret)
  1298. return ret;
  1299. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1300. if (&obj->base == NULL) {
  1301. ret = -ENOENT;
  1302. goto unlock;
  1303. }
  1304. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1305. ret = -E2BIG;
  1306. goto out;
  1307. }
  1308. if (obj->madv != I915_MADV_WILLNEED) {
  1309. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1310. ret = -EINVAL;
  1311. goto out;
  1312. }
  1313. ret = i915_gem_object_create_mmap_offset(obj);
  1314. if (ret)
  1315. goto out;
  1316. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1317. out:
  1318. drm_gem_object_unreference(&obj->base);
  1319. unlock:
  1320. mutex_unlock(&dev->struct_mutex);
  1321. return ret;
  1322. }
  1323. /**
  1324. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1325. * @dev: DRM device
  1326. * @data: GTT mapping ioctl data
  1327. * @file: GEM object info
  1328. *
  1329. * Simply returns the fake offset to userspace so it can mmap it.
  1330. * The mmap call will end up in drm_gem_mmap(), which will set things
  1331. * up so we can get faults in the handler above.
  1332. *
  1333. * The fault handler will take care of binding the object into the GTT
  1334. * (since it may have been evicted to make room for something), allocating
  1335. * a fence register, and mapping the appropriate aperture address into
  1336. * userspace.
  1337. */
  1338. int
  1339. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1340. struct drm_file *file)
  1341. {
  1342. struct drm_i915_gem_mmap_gtt *args = data;
  1343. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1344. }
  1345. /* Immediately discard the backing storage */
  1346. static void
  1347. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1348. {
  1349. struct inode *inode;
  1350. i915_gem_object_free_mmap_offset(obj);
  1351. if (obj->base.filp == NULL)
  1352. return;
  1353. /* Our goal here is to return as much of the memory as
  1354. * is possible back to the system as we are called from OOM.
  1355. * To do this we must instruct the shmfs to drop all of its
  1356. * backing pages, *now*.
  1357. */
  1358. inode = file_inode(obj->base.filp);
  1359. shmem_truncate_range(inode, 0, (loff_t)-1);
  1360. obj->madv = __I915_MADV_PURGED;
  1361. }
  1362. static inline int
  1363. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1364. {
  1365. return obj->madv == I915_MADV_DONTNEED;
  1366. }
  1367. static void
  1368. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1369. {
  1370. struct sg_page_iter sg_iter;
  1371. int ret;
  1372. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1373. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1374. if (ret) {
  1375. /* In the event of a disaster, abandon all caches and
  1376. * hope for the best.
  1377. */
  1378. WARN_ON(ret != -EIO);
  1379. i915_gem_clflush_object(obj);
  1380. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1381. }
  1382. if (i915_gem_object_needs_bit17_swizzle(obj))
  1383. i915_gem_object_save_bit_17_swizzle(obj);
  1384. if (obj->madv == I915_MADV_DONTNEED)
  1385. obj->dirty = 0;
  1386. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1387. struct page *page = sg_page_iter_page(&sg_iter);
  1388. if (obj->dirty)
  1389. set_page_dirty(page);
  1390. if (obj->madv == I915_MADV_WILLNEED)
  1391. mark_page_accessed(page);
  1392. page_cache_release(page);
  1393. }
  1394. obj->dirty = 0;
  1395. sg_free_table(obj->pages);
  1396. kfree(obj->pages);
  1397. }
  1398. int
  1399. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1400. {
  1401. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1402. if (obj->pages == NULL)
  1403. return 0;
  1404. BUG_ON(i915_gem_obj_ggtt_bound(obj));
  1405. if (obj->pages_pin_count)
  1406. return -EBUSY;
  1407. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1408. * array, hence protect them from being reaped by removing them from gtt
  1409. * lists early. */
  1410. list_del(&obj->global_list);
  1411. ops->put_pages(obj);
  1412. obj->pages = NULL;
  1413. if (i915_gem_object_is_purgeable(obj))
  1414. i915_gem_object_truncate(obj);
  1415. return 0;
  1416. }
  1417. static long
  1418. __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
  1419. bool purgeable_only)
  1420. {
  1421. struct drm_i915_gem_object *obj, *next;
  1422. struct i915_address_space *vm = &dev_priv->gtt.base;
  1423. long count = 0;
  1424. list_for_each_entry_safe(obj, next,
  1425. &dev_priv->mm.unbound_list,
  1426. global_list) {
  1427. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1428. i915_gem_object_put_pages(obj) == 0) {
  1429. count += obj->base.size >> PAGE_SHIFT;
  1430. if (count >= target)
  1431. return count;
  1432. }
  1433. }
  1434. list_for_each_entry_safe(obj, next, &vm->inactive_list, mm_list) {
  1435. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1436. i915_gem_object_unbind(obj) == 0 &&
  1437. i915_gem_object_put_pages(obj) == 0) {
  1438. count += obj->base.size >> PAGE_SHIFT;
  1439. if (count >= target)
  1440. return count;
  1441. }
  1442. }
  1443. return count;
  1444. }
  1445. static long
  1446. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1447. {
  1448. return __i915_gem_shrink(dev_priv, target, true);
  1449. }
  1450. static void
  1451. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1452. {
  1453. struct drm_i915_gem_object *obj, *next;
  1454. i915_gem_evict_everything(dev_priv->dev);
  1455. list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
  1456. global_list)
  1457. i915_gem_object_put_pages(obj);
  1458. }
  1459. static int
  1460. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1461. {
  1462. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1463. int page_count, i;
  1464. struct address_space *mapping;
  1465. struct sg_table *st;
  1466. struct scatterlist *sg;
  1467. struct sg_page_iter sg_iter;
  1468. struct page *page;
  1469. unsigned long last_pfn = 0; /* suppress gcc warning */
  1470. gfp_t gfp;
  1471. /* Assert that the object is not currently in any GPU domain. As it
  1472. * wasn't in the GTT, there shouldn't be any way it could have been in
  1473. * a GPU cache
  1474. */
  1475. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1476. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1477. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1478. if (st == NULL)
  1479. return -ENOMEM;
  1480. page_count = obj->base.size / PAGE_SIZE;
  1481. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1482. sg_free_table(st);
  1483. kfree(st);
  1484. return -ENOMEM;
  1485. }
  1486. /* Get the list of pages out of our struct file. They'll be pinned
  1487. * at this point until we release them.
  1488. *
  1489. * Fail silently without starting the shrinker
  1490. */
  1491. mapping = file_inode(obj->base.filp)->i_mapping;
  1492. gfp = mapping_gfp_mask(mapping);
  1493. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1494. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1495. sg = st->sgl;
  1496. st->nents = 0;
  1497. for (i = 0; i < page_count; i++) {
  1498. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1499. if (IS_ERR(page)) {
  1500. i915_gem_purge(dev_priv, page_count);
  1501. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1502. }
  1503. if (IS_ERR(page)) {
  1504. /* We've tried hard to allocate the memory by reaping
  1505. * our own buffer, now let the real VM do its job and
  1506. * go down in flames if truly OOM.
  1507. */
  1508. gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
  1509. gfp |= __GFP_IO | __GFP_WAIT;
  1510. i915_gem_shrink_all(dev_priv);
  1511. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1512. if (IS_ERR(page))
  1513. goto err_pages;
  1514. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1515. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1516. }
  1517. #ifdef CONFIG_SWIOTLB
  1518. if (swiotlb_nr_tbl()) {
  1519. st->nents++;
  1520. sg_set_page(sg, page, PAGE_SIZE, 0);
  1521. sg = sg_next(sg);
  1522. continue;
  1523. }
  1524. #endif
  1525. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1526. if (i)
  1527. sg = sg_next(sg);
  1528. st->nents++;
  1529. sg_set_page(sg, page, PAGE_SIZE, 0);
  1530. } else {
  1531. sg->length += PAGE_SIZE;
  1532. }
  1533. last_pfn = page_to_pfn(page);
  1534. }
  1535. #ifdef CONFIG_SWIOTLB
  1536. if (!swiotlb_nr_tbl())
  1537. #endif
  1538. sg_mark_end(sg);
  1539. obj->pages = st;
  1540. if (i915_gem_object_needs_bit17_swizzle(obj))
  1541. i915_gem_object_do_bit_17_swizzle(obj);
  1542. return 0;
  1543. err_pages:
  1544. sg_mark_end(sg);
  1545. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1546. page_cache_release(sg_page_iter_page(&sg_iter));
  1547. sg_free_table(st);
  1548. kfree(st);
  1549. return PTR_ERR(page);
  1550. }
  1551. /* Ensure that the associated pages are gathered from the backing storage
  1552. * and pinned into our object. i915_gem_object_get_pages() may be called
  1553. * multiple times before they are released by a single call to
  1554. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1555. * either as a result of memory pressure (reaping pages under the shrinker)
  1556. * or as the object is itself released.
  1557. */
  1558. int
  1559. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1560. {
  1561. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1562. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1563. int ret;
  1564. if (obj->pages)
  1565. return 0;
  1566. if (obj->madv != I915_MADV_WILLNEED) {
  1567. DRM_ERROR("Attempting to obtain a purgeable object\n");
  1568. return -EINVAL;
  1569. }
  1570. BUG_ON(obj->pages_pin_count);
  1571. ret = ops->get_pages(obj);
  1572. if (ret)
  1573. return ret;
  1574. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1575. return 0;
  1576. }
  1577. void
  1578. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1579. struct intel_ring_buffer *ring)
  1580. {
  1581. struct drm_device *dev = obj->base.dev;
  1582. struct drm_i915_private *dev_priv = dev->dev_private;
  1583. struct i915_address_space *vm = &dev_priv->gtt.base;
  1584. u32 seqno = intel_ring_get_seqno(ring);
  1585. BUG_ON(ring == NULL);
  1586. obj->ring = ring;
  1587. /* Add a reference if we're newly entering the active list. */
  1588. if (!obj->active) {
  1589. drm_gem_object_reference(&obj->base);
  1590. obj->active = 1;
  1591. }
  1592. /* Move from whatever list we were on to the tail of execution. */
  1593. list_move_tail(&obj->mm_list, &vm->active_list);
  1594. list_move_tail(&obj->ring_list, &ring->active_list);
  1595. obj->last_read_seqno = seqno;
  1596. if (obj->fenced_gpu_access) {
  1597. obj->last_fenced_seqno = seqno;
  1598. /* Bump MRU to take account of the delayed flush */
  1599. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1600. struct drm_i915_fence_reg *reg;
  1601. reg = &dev_priv->fence_regs[obj->fence_reg];
  1602. list_move_tail(&reg->lru_list,
  1603. &dev_priv->mm.fence_list);
  1604. }
  1605. }
  1606. }
  1607. static void
  1608. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1609. {
  1610. struct drm_device *dev = obj->base.dev;
  1611. struct drm_i915_private *dev_priv = dev->dev_private;
  1612. struct i915_address_space *vm = &dev_priv->gtt.base;
  1613. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1614. BUG_ON(!obj->active);
  1615. list_move_tail(&obj->mm_list, &vm->inactive_list);
  1616. list_del_init(&obj->ring_list);
  1617. obj->ring = NULL;
  1618. obj->last_read_seqno = 0;
  1619. obj->last_write_seqno = 0;
  1620. obj->base.write_domain = 0;
  1621. obj->last_fenced_seqno = 0;
  1622. obj->fenced_gpu_access = false;
  1623. obj->active = 0;
  1624. drm_gem_object_unreference(&obj->base);
  1625. WARN_ON(i915_verify_lists(dev));
  1626. }
  1627. static int
  1628. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1629. {
  1630. struct drm_i915_private *dev_priv = dev->dev_private;
  1631. struct intel_ring_buffer *ring;
  1632. int ret, i, j;
  1633. /* Carefully retire all requests without writing to the rings */
  1634. for_each_ring(ring, dev_priv, i) {
  1635. ret = intel_ring_idle(ring);
  1636. if (ret)
  1637. return ret;
  1638. }
  1639. i915_gem_retire_requests(dev);
  1640. /* Finally reset hw state */
  1641. for_each_ring(ring, dev_priv, i) {
  1642. intel_ring_init_seqno(ring, seqno);
  1643. for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
  1644. ring->sync_seqno[j] = 0;
  1645. }
  1646. return 0;
  1647. }
  1648. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1649. {
  1650. struct drm_i915_private *dev_priv = dev->dev_private;
  1651. int ret;
  1652. if (seqno == 0)
  1653. return -EINVAL;
  1654. /* HWS page needs to be set less than what we
  1655. * will inject to ring
  1656. */
  1657. ret = i915_gem_init_seqno(dev, seqno - 1);
  1658. if (ret)
  1659. return ret;
  1660. /* Carefully set the last_seqno value so that wrap
  1661. * detection still works
  1662. */
  1663. dev_priv->next_seqno = seqno;
  1664. dev_priv->last_seqno = seqno - 1;
  1665. if (dev_priv->last_seqno == 0)
  1666. dev_priv->last_seqno--;
  1667. return 0;
  1668. }
  1669. int
  1670. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1671. {
  1672. struct drm_i915_private *dev_priv = dev->dev_private;
  1673. /* reserve 0 for non-seqno */
  1674. if (dev_priv->next_seqno == 0) {
  1675. int ret = i915_gem_init_seqno(dev, 0);
  1676. if (ret)
  1677. return ret;
  1678. dev_priv->next_seqno = 1;
  1679. }
  1680. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1681. return 0;
  1682. }
  1683. int __i915_add_request(struct intel_ring_buffer *ring,
  1684. struct drm_file *file,
  1685. struct drm_i915_gem_object *obj,
  1686. u32 *out_seqno)
  1687. {
  1688. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1689. struct drm_i915_gem_request *request;
  1690. u32 request_ring_position, request_start;
  1691. int was_empty;
  1692. int ret;
  1693. request_start = intel_ring_get_tail(ring);
  1694. /*
  1695. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1696. * after having emitted the batchbuffer command. Hence we need to fix
  1697. * things up similar to emitting the lazy request. The difference here
  1698. * is that the flush _must_ happen before the next request, no matter
  1699. * what.
  1700. */
  1701. ret = intel_ring_flush_all_caches(ring);
  1702. if (ret)
  1703. return ret;
  1704. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1705. if (request == NULL)
  1706. return -ENOMEM;
  1707. /* Record the position of the start of the request so that
  1708. * should we detect the updated seqno part-way through the
  1709. * GPU processing the request, we never over-estimate the
  1710. * position of the head.
  1711. */
  1712. request_ring_position = intel_ring_get_tail(ring);
  1713. ret = ring->add_request(ring);
  1714. if (ret) {
  1715. kfree(request);
  1716. return ret;
  1717. }
  1718. request->seqno = intel_ring_get_seqno(ring);
  1719. request->ring = ring;
  1720. request->head = request_start;
  1721. request->tail = request_ring_position;
  1722. request->ctx = ring->last_context;
  1723. request->batch_obj = obj;
  1724. /* Whilst this request exists, batch_obj will be on the
  1725. * active_list, and so will hold the active reference. Only when this
  1726. * request is retired will the the batch_obj be moved onto the
  1727. * inactive_list and lose its active reference. Hence we do not need
  1728. * to explicitly hold another reference here.
  1729. */
  1730. if (request->ctx)
  1731. i915_gem_context_reference(request->ctx);
  1732. request->emitted_jiffies = jiffies;
  1733. was_empty = list_empty(&ring->request_list);
  1734. list_add_tail(&request->list, &ring->request_list);
  1735. request->file_priv = NULL;
  1736. if (file) {
  1737. struct drm_i915_file_private *file_priv = file->driver_priv;
  1738. spin_lock(&file_priv->mm.lock);
  1739. request->file_priv = file_priv;
  1740. list_add_tail(&request->client_list,
  1741. &file_priv->mm.request_list);
  1742. spin_unlock(&file_priv->mm.lock);
  1743. }
  1744. trace_i915_gem_request_add(ring, request->seqno);
  1745. ring->outstanding_lazy_request = 0;
  1746. if (!dev_priv->ums.mm_suspended) {
  1747. i915_queue_hangcheck(ring->dev);
  1748. if (was_empty) {
  1749. queue_delayed_work(dev_priv->wq,
  1750. &dev_priv->mm.retire_work,
  1751. round_jiffies_up_relative(HZ));
  1752. intel_mark_busy(dev_priv->dev);
  1753. }
  1754. }
  1755. if (out_seqno)
  1756. *out_seqno = request->seqno;
  1757. return 0;
  1758. }
  1759. static inline void
  1760. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1761. {
  1762. struct drm_i915_file_private *file_priv = request->file_priv;
  1763. if (!file_priv)
  1764. return;
  1765. spin_lock(&file_priv->mm.lock);
  1766. if (request->file_priv) {
  1767. list_del(&request->client_list);
  1768. request->file_priv = NULL;
  1769. }
  1770. spin_unlock(&file_priv->mm.lock);
  1771. }
  1772. static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj)
  1773. {
  1774. if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
  1775. acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
  1776. return true;
  1777. return false;
  1778. }
  1779. static bool i915_head_inside_request(const u32 acthd_unmasked,
  1780. const u32 request_start,
  1781. const u32 request_end)
  1782. {
  1783. const u32 acthd = acthd_unmasked & HEAD_ADDR;
  1784. if (request_start < request_end) {
  1785. if (acthd >= request_start && acthd < request_end)
  1786. return true;
  1787. } else if (request_start > request_end) {
  1788. if (acthd >= request_start || acthd < request_end)
  1789. return true;
  1790. }
  1791. return false;
  1792. }
  1793. static bool i915_request_guilty(struct drm_i915_gem_request *request,
  1794. const u32 acthd, bool *inside)
  1795. {
  1796. /* There is a possibility that unmasked head address
  1797. * pointing inside the ring, matches the batch_obj address range.
  1798. * However this is extremely unlikely.
  1799. */
  1800. if (request->batch_obj) {
  1801. if (i915_head_inside_object(acthd, request->batch_obj)) {
  1802. *inside = true;
  1803. return true;
  1804. }
  1805. }
  1806. if (i915_head_inside_request(acthd, request->head, request->tail)) {
  1807. *inside = false;
  1808. return true;
  1809. }
  1810. return false;
  1811. }
  1812. static void i915_set_reset_status(struct intel_ring_buffer *ring,
  1813. struct drm_i915_gem_request *request,
  1814. u32 acthd)
  1815. {
  1816. struct i915_ctx_hang_stats *hs = NULL;
  1817. bool inside, guilty;
  1818. /* Innocent until proven guilty */
  1819. guilty = false;
  1820. if (ring->hangcheck.action != wait &&
  1821. i915_request_guilty(request, acthd, &inside)) {
  1822. DRM_ERROR("%s hung %s bo (0x%lx ctx %d) at 0x%x\n",
  1823. ring->name,
  1824. inside ? "inside" : "flushing",
  1825. request->batch_obj ?
  1826. i915_gem_obj_ggtt_offset(request->batch_obj) : 0,
  1827. request->ctx ? request->ctx->id : 0,
  1828. acthd);
  1829. guilty = true;
  1830. }
  1831. /* If contexts are disabled or this is the default context, use
  1832. * file_priv->reset_state
  1833. */
  1834. if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID)
  1835. hs = &request->ctx->hang_stats;
  1836. else if (request->file_priv)
  1837. hs = &request->file_priv->hang_stats;
  1838. if (hs) {
  1839. if (guilty)
  1840. hs->batch_active++;
  1841. else
  1842. hs->batch_pending++;
  1843. }
  1844. }
  1845. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  1846. {
  1847. list_del(&request->list);
  1848. i915_gem_request_remove_from_client(request);
  1849. if (request->ctx)
  1850. i915_gem_context_unreference(request->ctx);
  1851. kfree(request);
  1852. }
  1853. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1854. struct intel_ring_buffer *ring)
  1855. {
  1856. u32 completed_seqno;
  1857. u32 acthd;
  1858. acthd = intel_ring_get_active_head(ring);
  1859. completed_seqno = ring->get_seqno(ring, false);
  1860. while (!list_empty(&ring->request_list)) {
  1861. struct drm_i915_gem_request *request;
  1862. request = list_first_entry(&ring->request_list,
  1863. struct drm_i915_gem_request,
  1864. list);
  1865. if (request->seqno > completed_seqno)
  1866. i915_set_reset_status(ring, request, acthd);
  1867. i915_gem_free_request(request);
  1868. }
  1869. while (!list_empty(&ring->active_list)) {
  1870. struct drm_i915_gem_object *obj;
  1871. obj = list_first_entry(&ring->active_list,
  1872. struct drm_i915_gem_object,
  1873. ring_list);
  1874. i915_gem_object_move_to_inactive(obj);
  1875. }
  1876. }
  1877. static void i915_gem_reset_fences(struct drm_device *dev)
  1878. {
  1879. struct drm_i915_private *dev_priv = dev->dev_private;
  1880. int i;
  1881. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1882. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1883. if (reg->obj)
  1884. i915_gem_object_fence_lost(reg->obj);
  1885. i915_gem_write_fence(dev, i, NULL);
  1886. reg->pin_count = 0;
  1887. reg->obj = NULL;
  1888. INIT_LIST_HEAD(&reg->lru_list);
  1889. }
  1890. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  1891. }
  1892. void i915_gem_reset(struct drm_device *dev)
  1893. {
  1894. struct drm_i915_private *dev_priv = dev->dev_private;
  1895. struct i915_address_space *vm = &dev_priv->gtt.base;
  1896. struct drm_i915_gem_object *obj;
  1897. struct intel_ring_buffer *ring;
  1898. int i;
  1899. for_each_ring(ring, dev_priv, i)
  1900. i915_gem_reset_ring_lists(dev_priv, ring);
  1901. /* Move everything out of the GPU domains to ensure we do any
  1902. * necessary invalidation upon reuse.
  1903. */
  1904. list_for_each_entry(obj, &vm->inactive_list, mm_list)
  1905. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1906. /* The fence registers are invalidated so clear them out */
  1907. i915_gem_reset_fences(dev);
  1908. }
  1909. /**
  1910. * This function clears the request list as sequence numbers are passed.
  1911. */
  1912. void
  1913. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1914. {
  1915. uint32_t seqno;
  1916. if (list_empty(&ring->request_list))
  1917. return;
  1918. WARN_ON(i915_verify_lists(ring->dev));
  1919. seqno = ring->get_seqno(ring, true);
  1920. while (!list_empty(&ring->request_list)) {
  1921. struct drm_i915_gem_request *request;
  1922. request = list_first_entry(&ring->request_list,
  1923. struct drm_i915_gem_request,
  1924. list);
  1925. if (!i915_seqno_passed(seqno, request->seqno))
  1926. break;
  1927. trace_i915_gem_request_retire(ring, request->seqno);
  1928. /* We know the GPU must have read the request to have
  1929. * sent us the seqno + interrupt, so use the position
  1930. * of tail of the request to update the last known position
  1931. * of the GPU head.
  1932. */
  1933. ring->last_retired_head = request->tail;
  1934. i915_gem_free_request(request);
  1935. }
  1936. /* Move any buffers on the active list that are no longer referenced
  1937. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1938. */
  1939. while (!list_empty(&ring->active_list)) {
  1940. struct drm_i915_gem_object *obj;
  1941. obj = list_first_entry(&ring->active_list,
  1942. struct drm_i915_gem_object,
  1943. ring_list);
  1944. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  1945. break;
  1946. i915_gem_object_move_to_inactive(obj);
  1947. }
  1948. if (unlikely(ring->trace_irq_seqno &&
  1949. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1950. ring->irq_put(ring);
  1951. ring->trace_irq_seqno = 0;
  1952. }
  1953. WARN_ON(i915_verify_lists(ring->dev));
  1954. }
  1955. void
  1956. i915_gem_retire_requests(struct drm_device *dev)
  1957. {
  1958. drm_i915_private_t *dev_priv = dev->dev_private;
  1959. struct intel_ring_buffer *ring;
  1960. int i;
  1961. for_each_ring(ring, dev_priv, i)
  1962. i915_gem_retire_requests_ring(ring);
  1963. }
  1964. static void
  1965. i915_gem_retire_work_handler(struct work_struct *work)
  1966. {
  1967. drm_i915_private_t *dev_priv;
  1968. struct drm_device *dev;
  1969. struct intel_ring_buffer *ring;
  1970. bool idle;
  1971. int i;
  1972. dev_priv = container_of(work, drm_i915_private_t,
  1973. mm.retire_work.work);
  1974. dev = dev_priv->dev;
  1975. /* Come back later if the device is busy... */
  1976. if (!mutex_trylock(&dev->struct_mutex)) {
  1977. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1978. round_jiffies_up_relative(HZ));
  1979. return;
  1980. }
  1981. i915_gem_retire_requests(dev);
  1982. /* Send a periodic flush down the ring so we don't hold onto GEM
  1983. * objects indefinitely.
  1984. */
  1985. idle = true;
  1986. for_each_ring(ring, dev_priv, i) {
  1987. if (ring->gpu_caches_dirty)
  1988. i915_add_request(ring, NULL);
  1989. idle &= list_empty(&ring->request_list);
  1990. }
  1991. if (!dev_priv->ums.mm_suspended && !idle)
  1992. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  1993. round_jiffies_up_relative(HZ));
  1994. if (idle)
  1995. intel_mark_idle(dev);
  1996. mutex_unlock(&dev->struct_mutex);
  1997. }
  1998. /**
  1999. * Ensures that an object will eventually get non-busy by flushing any required
  2000. * write domains, emitting any outstanding lazy request and retiring and
  2001. * completed requests.
  2002. */
  2003. static int
  2004. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2005. {
  2006. int ret;
  2007. if (obj->active) {
  2008. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2009. if (ret)
  2010. return ret;
  2011. i915_gem_retire_requests_ring(obj->ring);
  2012. }
  2013. return 0;
  2014. }
  2015. /**
  2016. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2017. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2018. *
  2019. * Returns 0 if successful, else an error is returned with the remaining time in
  2020. * the timeout parameter.
  2021. * -ETIME: object is still busy after timeout
  2022. * -ERESTARTSYS: signal interrupted the wait
  2023. * -ENONENT: object doesn't exist
  2024. * Also possible, but rare:
  2025. * -EAGAIN: GPU wedged
  2026. * -ENOMEM: damn
  2027. * -ENODEV: Internal IRQ fail
  2028. * -E?: The add request failed
  2029. *
  2030. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2031. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2032. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2033. * without holding struct_mutex the object may become re-busied before this
  2034. * function completes. A similar but shorter * race condition exists in the busy
  2035. * ioctl
  2036. */
  2037. int
  2038. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2039. {
  2040. drm_i915_private_t *dev_priv = dev->dev_private;
  2041. struct drm_i915_gem_wait *args = data;
  2042. struct drm_i915_gem_object *obj;
  2043. struct intel_ring_buffer *ring = NULL;
  2044. struct timespec timeout_stack, *timeout = NULL;
  2045. unsigned reset_counter;
  2046. u32 seqno = 0;
  2047. int ret = 0;
  2048. if (args->timeout_ns >= 0) {
  2049. timeout_stack = ns_to_timespec(args->timeout_ns);
  2050. timeout = &timeout_stack;
  2051. }
  2052. ret = i915_mutex_lock_interruptible(dev);
  2053. if (ret)
  2054. return ret;
  2055. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2056. if (&obj->base == NULL) {
  2057. mutex_unlock(&dev->struct_mutex);
  2058. return -ENOENT;
  2059. }
  2060. /* Need to make sure the object gets inactive eventually. */
  2061. ret = i915_gem_object_flush_active(obj);
  2062. if (ret)
  2063. goto out;
  2064. if (obj->active) {
  2065. seqno = obj->last_read_seqno;
  2066. ring = obj->ring;
  2067. }
  2068. if (seqno == 0)
  2069. goto out;
  2070. /* Do this after OLR check to make sure we make forward progress polling
  2071. * on this IOCTL with a 0 timeout (like busy ioctl)
  2072. */
  2073. if (!args->timeout_ns) {
  2074. ret = -ETIME;
  2075. goto out;
  2076. }
  2077. drm_gem_object_unreference(&obj->base);
  2078. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2079. mutex_unlock(&dev->struct_mutex);
  2080. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
  2081. if (timeout)
  2082. args->timeout_ns = timespec_to_ns(timeout);
  2083. return ret;
  2084. out:
  2085. drm_gem_object_unreference(&obj->base);
  2086. mutex_unlock(&dev->struct_mutex);
  2087. return ret;
  2088. }
  2089. /**
  2090. * i915_gem_object_sync - sync an object to a ring.
  2091. *
  2092. * @obj: object which may be in use on another ring.
  2093. * @to: ring we wish to use the object on. May be NULL.
  2094. *
  2095. * This code is meant to abstract object synchronization with the GPU.
  2096. * Calling with NULL implies synchronizing the object with the CPU
  2097. * rather than a particular GPU ring.
  2098. *
  2099. * Returns 0 if successful, else propagates up the lower layer error.
  2100. */
  2101. int
  2102. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2103. struct intel_ring_buffer *to)
  2104. {
  2105. struct intel_ring_buffer *from = obj->ring;
  2106. u32 seqno;
  2107. int ret, idx;
  2108. if (from == NULL || to == from)
  2109. return 0;
  2110. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2111. return i915_gem_object_wait_rendering(obj, false);
  2112. idx = intel_ring_sync_index(from, to);
  2113. seqno = obj->last_read_seqno;
  2114. if (seqno <= from->sync_seqno[idx])
  2115. return 0;
  2116. ret = i915_gem_check_olr(obj->ring, seqno);
  2117. if (ret)
  2118. return ret;
  2119. ret = to->sync_to(to, from, seqno);
  2120. if (!ret)
  2121. /* We use last_read_seqno because sync_to()
  2122. * might have just caused seqno wrap under
  2123. * the radar.
  2124. */
  2125. from->sync_seqno[idx] = obj->last_read_seqno;
  2126. return ret;
  2127. }
  2128. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2129. {
  2130. u32 old_write_domain, old_read_domains;
  2131. /* Force a pagefault for domain tracking on next user access */
  2132. i915_gem_release_mmap(obj);
  2133. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2134. return;
  2135. /* Wait for any direct GTT access to complete */
  2136. mb();
  2137. old_read_domains = obj->base.read_domains;
  2138. old_write_domain = obj->base.write_domain;
  2139. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2140. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2141. trace_i915_gem_object_change_domain(obj,
  2142. old_read_domains,
  2143. old_write_domain);
  2144. }
  2145. /**
  2146. * Unbinds an object from the GTT aperture.
  2147. */
  2148. int
  2149. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  2150. {
  2151. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2152. struct i915_vma *vma;
  2153. int ret;
  2154. if (!i915_gem_obj_ggtt_bound(obj))
  2155. return 0;
  2156. if (obj->pin_count)
  2157. return -EBUSY;
  2158. BUG_ON(obj->pages == NULL);
  2159. ret = i915_gem_object_finish_gpu(obj);
  2160. if (ret)
  2161. return ret;
  2162. /* Continue on if we fail due to EIO, the GPU is hung so we
  2163. * should be safe and we need to cleanup or else we might
  2164. * cause memory corruption through use-after-free.
  2165. */
  2166. i915_gem_object_finish_gtt(obj);
  2167. /* release the fence reg _after_ flushing */
  2168. ret = i915_gem_object_put_fence(obj);
  2169. if (ret)
  2170. return ret;
  2171. trace_i915_gem_object_unbind(obj);
  2172. if (obj->has_global_gtt_mapping)
  2173. i915_gem_gtt_unbind_object(obj);
  2174. if (obj->has_aliasing_ppgtt_mapping) {
  2175. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2176. obj->has_aliasing_ppgtt_mapping = 0;
  2177. }
  2178. i915_gem_gtt_finish_object(obj);
  2179. i915_gem_object_unpin_pages(obj);
  2180. list_del(&obj->mm_list);
  2181. /* Avoid an unnecessary call to unbind on rebind. */
  2182. obj->map_and_fenceable = true;
  2183. vma = __i915_gem_obj_to_vma(obj);
  2184. list_del(&vma->vma_link);
  2185. drm_mm_remove_node(&vma->node);
  2186. i915_gem_vma_destroy(vma);
  2187. /* Since the unbound list is global, only move to that list if
  2188. * no more VMAs exist.
  2189. * NB: Until we have real VMAs there will only ever be one */
  2190. WARN_ON(!list_empty(&obj->vma_list));
  2191. if (list_empty(&obj->vma_list))
  2192. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2193. return 0;
  2194. }
  2195. int i915_gpu_idle(struct drm_device *dev)
  2196. {
  2197. drm_i915_private_t *dev_priv = dev->dev_private;
  2198. struct intel_ring_buffer *ring;
  2199. int ret, i;
  2200. /* Flush everything onto the inactive list. */
  2201. for_each_ring(ring, dev_priv, i) {
  2202. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2203. if (ret)
  2204. return ret;
  2205. ret = intel_ring_idle(ring);
  2206. if (ret)
  2207. return ret;
  2208. }
  2209. return 0;
  2210. }
  2211. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2212. struct drm_i915_gem_object *obj)
  2213. {
  2214. drm_i915_private_t *dev_priv = dev->dev_private;
  2215. int fence_reg;
  2216. int fence_pitch_shift;
  2217. uint64_t val;
  2218. if (INTEL_INFO(dev)->gen >= 6) {
  2219. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2220. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2221. } else {
  2222. fence_reg = FENCE_REG_965_0;
  2223. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2224. }
  2225. if (obj) {
  2226. u32 size = i915_gem_obj_ggtt_size(obj);
  2227. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2228. 0xfffff000) << 32;
  2229. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2230. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2231. if (obj->tiling_mode == I915_TILING_Y)
  2232. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2233. val |= I965_FENCE_REG_VALID;
  2234. } else
  2235. val = 0;
  2236. fence_reg += reg * 8;
  2237. I915_WRITE64(fence_reg, val);
  2238. POSTING_READ(fence_reg);
  2239. }
  2240. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2241. struct drm_i915_gem_object *obj)
  2242. {
  2243. drm_i915_private_t *dev_priv = dev->dev_private;
  2244. u32 val;
  2245. if (obj) {
  2246. u32 size = i915_gem_obj_ggtt_size(obj);
  2247. int pitch_val;
  2248. int tile_width;
  2249. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2250. (size & -size) != size ||
  2251. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2252. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2253. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2254. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2255. tile_width = 128;
  2256. else
  2257. tile_width = 512;
  2258. /* Note: pitch better be a power of two tile widths */
  2259. pitch_val = obj->stride / tile_width;
  2260. pitch_val = ffs(pitch_val) - 1;
  2261. val = i915_gem_obj_ggtt_offset(obj);
  2262. if (obj->tiling_mode == I915_TILING_Y)
  2263. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2264. val |= I915_FENCE_SIZE_BITS(size);
  2265. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2266. val |= I830_FENCE_REG_VALID;
  2267. } else
  2268. val = 0;
  2269. if (reg < 8)
  2270. reg = FENCE_REG_830_0 + reg * 4;
  2271. else
  2272. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2273. I915_WRITE(reg, val);
  2274. POSTING_READ(reg);
  2275. }
  2276. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2277. struct drm_i915_gem_object *obj)
  2278. {
  2279. drm_i915_private_t *dev_priv = dev->dev_private;
  2280. uint32_t val;
  2281. if (obj) {
  2282. u32 size = i915_gem_obj_ggtt_size(obj);
  2283. uint32_t pitch_val;
  2284. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2285. (size & -size) != size ||
  2286. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2287. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2288. i915_gem_obj_ggtt_offset(obj), size);
  2289. pitch_val = obj->stride / 128;
  2290. pitch_val = ffs(pitch_val) - 1;
  2291. val = i915_gem_obj_ggtt_offset(obj);
  2292. if (obj->tiling_mode == I915_TILING_Y)
  2293. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2294. val |= I830_FENCE_SIZE_BITS(size);
  2295. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2296. val |= I830_FENCE_REG_VALID;
  2297. } else
  2298. val = 0;
  2299. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2300. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2301. }
  2302. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2303. {
  2304. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2305. }
  2306. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2307. struct drm_i915_gem_object *obj)
  2308. {
  2309. struct drm_i915_private *dev_priv = dev->dev_private;
  2310. /* Ensure that all CPU reads are completed before installing a fence
  2311. * and all writes before removing the fence.
  2312. */
  2313. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2314. mb();
  2315. switch (INTEL_INFO(dev)->gen) {
  2316. case 7:
  2317. case 6:
  2318. case 5:
  2319. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2320. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2321. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2322. default: BUG();
  2323. }
  2324. /* And similarly be paranoid that no direct access to this region
  2325. * is reordered to before the fence is installed.
  2326. */
  2327. if (i915_gem_object_needs_mb(obj))
  2328. mb();
  2329. }
  2330. static inline int fence_number(struct drm_i915_private *dev_priv,
  2331. struct drm_i915_fence_reg *fence)
  2332. {
  2333. return fence - dev_priv->fence_regs;
  2334. }
  2335. struct write_fence {
  2336. struct drm_device *dev;
  2337. struct drm_i915_gem_object *obj;
  2338. int fence;
  2339. };
  2340. static void i915_gem_write_fence__ipi(void *data)
  2341. {
  2342. struct write_fence *args = data;
  2343. /* Required for SNB+ with LLC */
  2344. wbinvd();
  2345. /* Required for VLV */
  2346. i915_gem_write_fence(args->dev, args->fence, args->obj);
  2347. }
  2348. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2349. struct drm_i915_fence_reg *fence,
  2350. bool enable)
  2351. {
  2352. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2353. struct write_fence args = {
  2354. .dev = obj->base.dev,
  2355. .fence = fence_number(dev_priv, fence),
  2356. .obj = enable ? obj : NULL,
  2357. };
  2358. /* In order to fully serialize access to the fenced region and
  2359. * the update to the fence register we need to take extreme
  2360. * measures on SNB+. In theory, the write to the fence register
  2361. * flushes all memory transactions before, and coupled with the
  2362. * mb() placed around the register write we serialise all memory
  2363. * operations with respect to the changes in the tiler. Yet, on
  2364. * SNB+ we need to take a step further and emit an explicit wbinvd()
  2365. * on each processor in order to manually flush all memory
  2366. * transactions before updating the fence register.
  2367. *
  2368. * However, Valleyview complicates matter. There the wbinvd is
  2369. * insufficient and unlike SNB/IVB requires the serialising
  2370. * register write. (Note that that register write by itself is
  2371. * conversely not sufficient for SNB+.) To compromise, we do both.
  2372. */
  2373. if (INTEL_INFO(args.dev)->gen >= 6)
  2374. on_each_cpu(i915_gem_write_fence__ipi, &args, 1);
  2375. else
  2376. i915_gem_write_fence(args.dev, args.fence, args.obj);
  2377. if (enable) {
  2378. obj->fence_reg = args.fence;
  2379. fence->obj = obj;
  2380. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2381. } else {
  2382. obj->fence_reg = I915_FENCE_REG_NONE;
  2383. fence->obj = NULL;
  2384. list_del_init(&fence->lru_list);
  2385. }
  2386. }
  2387. static int
  2388. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2389. {
  2390. if (obj->last_fenced_seqno) {
  2391. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2392. if (ret)
  2393. return ret;
  2394. obj->last_fenced_seqno = 0;
  2395. }
  2396. obj->fenced_gpu_access = false;
  2397. return 0;
  2398. }
  2399. int
  2400. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2401. {
  2402. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2403. struct drm_i915_fence_reg *fence;
  2404. int ret;
  2405. ret = i915_gem_object_wait_fence(obj);
  2406. if (ret)
  2407. return ret;
  2408. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2409. return 0;
  2410. fence = &dev_priv->fence_regs[obj->fence_reg];
  2411. i915_gem_object_fence_lost(obj);
  2412. i915_gem_object_update_fence(obj, fence, false);
  2413. return 0;
  2414. }
  2415. static struct drm_i915_fence_reg *
  2416. i915_find_fence_reg(struct drm_device *dev)
  2417. {
  2418. struct drm_i915_private *dev_priv = dev->dev_private;
  2419. struct drm_i915_fence_reg *reg, *avail;
  2420. int i;
  2421. /* First try to find a free reg */
  2422. avail = NULL;
  2423. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2424. reg = &dev_priv->fence_regs[i];
  2425. if (!reg->obj)
  2426. return reg;
  2427. if (!reg->pin_count)
  2428. avail = reg;
  2429. }
  2430. if (avail == NULL)
  2431. return NULL;
  2432. /* None available, try to steal one or wait for a user to finish */
  2433. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2434. if (reg->pin_count)
  2435. continue;
  2436. return reg;
  2437. }
  2438. return NULL;
  2439. }
  2440. /**
  2441. * i915_gem_object_get_fence - set up fencing for an object
  2442. * @obj: object to map through a fence reg
  2443. *
  2444. * When mapping objects through the GTT, userspace wants to be able to write
  2445. * to them without having to worry about swizzling if the object is tiled.
  2446. * This function walks the fence regs looking for a free one for @obj,
  2447. * stealing one if it can't find any.
  2448. *
  2449. * It then sets up the reg based on the object's properties: address, pitch
  2450. * and tiling format.
  2451. *
  2452. * For an untiled surface, this removes any existing fence.
  2453. */
  2454. int
  2455. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2456. {
  2457. struct drm_device *dev = obj->base.dev;
  2458. struct drm_i915_private *dev_priv = dev->dev_private;
  2459. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2460. struct drm_i915_fence_reg *reg;
  2461. int ret;
  2462. /* Have we updated the tiling parameters upon the object and so
  2463. * will need to serialise the write to the associated fence register?
  2464. */
  2465. if (obj->fence_dirty) {
  2466. ret = i915_gem_object_wait_fence(obj);
  2467. if (ret)
  2468. return ret;
  2469. }
  2470. /* Just update our place in the LRU if our fence is getting reused. */
  2471. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2472. reg = &dev_priv->fence_regs[obj->fence_reg];
  2473. if (!obj->fence_dirty) {
  2474. list_move_tail(&reg->lru_list,
  2475. &dev_priv->mm.fence_list);
  2476. return 0;
  2477. }
  2478. } else if (enable) {
  2479. reg = i915_find_fence_reg(dev);
  2480. if (reg == NULL)
  2481. return -EDEADLK;
  2482. if (reg->obj) {
  2483. struct drm_i915_gem_object *old = reg->obj;
  2484. ret = i915_gem_object_wait_fence(old);
  2485. if (ret)
  2486. return ret;
  2487. i915_gem_object_fence_lost(old);
  2488. }
  2489. } else
  2490. return 0;
  2491. i915_gem_object_update_fence(obj, reg, enable);
  2492. obj->fence_dirty = false;
  2493. return 0;
  2494. }
  2495. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2496. struct drm_mm_node *gtt_space,
  2497. unsigned long cache_level)
  2498. {
  2499. struct drm_mm_node *other;
  2500. /* On non-LLC machines we have to be careful when putting differing
  2501. * types of snoopable memory together to avoid the prefetcher
  2502. * crossing memory domains and dying.
  2503. */
  2504. if (HAS_LLC(dev))
  2505. return true;
  2506. if (!drm_mm_node_allocated(gtt_space))
  2507. return true;
  2508. if (list_empty(&gtt_space->node_list))
  2509. return true;
  2510. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2511. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2512. return false;
  2513. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2514. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2515. return false;
  2516. return true;
  2517. }
  2518. static void i915_gem_verify_gtt(struct drm_device *dev)
  2519. {
  2520. #if WATCH_GTT
  2521. struct drm_i915_private *dev_priv = dev->dev_private;
  2522. struct drm_i915_gem_object *obj;
  2523. int err = 0;
  2524. list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
  2525. if (obj->gtt_space == NULL) {
  2526. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2527. err++;
  2528. continue;
  2529. }
  2530. if (obj->cache_level != obj->gtt_space->color) {
  2531. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2532. i915_gem_obj_ggtt_offset(obj),
  2533. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2534. obj->cache_level,
  2535. obj->gtt_space->color);
  2536. err++;
  2537. continue;
  2538. }
  2539. if (!i915_gem_valid_gtt_space(dev,
  2540. obj->gtt_space,
  2541. obj->cache_level)) {
  2542. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2543. i915_gem_obj_ggtt_offset(obj),
  2544. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2545. obj->cache_level);
  2546. err++;
  2547. continue;
  2548. }
  2549. }
  2550. WARN_ON(err);
  2551. #endif
  2552. }
  2553. /**
  2554. * Finds free space in the GTT aperture and binds the object there.
  2555. */
  2556. static int
  2557. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2558. unsigned alignment,
  2559. bool map_and_fenceable,
  2560. bool nonblocking)
  2561. {
  2562. struct drm_device *dev = obj->base.dev;
  2563. drm_i915_private_t *dev_priv = dev->dev_private;
  2564. struct i915_address_space *vm = &dev_priv->gtt.base;
  2565. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2566. bool mappable, fenceable;
  2567. size_t gtt_max = map_and_fenceable ?
  2568. dev_priv->gtt.mappable_end : dev_priv->gtt.base.total;
  2569. struct i915_vma *vma;
  2570. int ret;
  2571. if (WARN_ON(!list_empty(&obj->vma_list)))
  2572. return -EBUSY;
  2573. fence_size = i915_gem_get_gtt_size(dev,
  2574. obj->base.size,
  2575. obj->tiling_mode);
  2576. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2577. obj->base.size,
  2578. obj->tiling_mode, true);
  2579. unfenced_alignment =
  2580. i915_gem_get_gtt_alignment(dev,
  2581. obj->base.size,
  2582. obj->tiling_mode, false);
  2583. if (alignment == 0)
  2584. alignment = map_and_fenceable ? fence_alignment :
  2585. unfenced_alignment;
  2586. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2587. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2588. return -EINVAL;
  2589. }
  2590. size = map_and_fenceable ? fence_size : obj->base.size;
  2591. /* If the object is bigger than the entire aperture, reject it early
  2592. * before evicting everything in a vain attempt to find space.
  2593. */
  2594. if (obj->base.size > gtt_max) {
  2595. DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
  2596. obj->base.size,
  2597. map_and_fenceable ? "mappable" : "total",
  2598. gtt_max);
  2599. return -E2BIG;
  2600. }
  2601. ret = i915_gem_object_get_pages(obj);
  2602. if (ret)
  2603. return ret;
  2604. i915_gem_object_pin_pages(obj);
  2605. vma = i915_gem_vma_create(obj, &dev_priv->gtt.base);
  2606. if (IS_ERR(vma)) {
  2607. ret = PTR_ERR(vma);
  2608. goto err_unpin;
  2609. }
  2610. search_free:
  2611. ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
  2612. &vma->node,
  2613. size, alignment,
  2614. obj->cache_level, 0, gtt_max);
  2615. if (ret) {
  2616. ret = i915_gem_evict_something(dev, size, alignment,
  2617. obj->cache_level,
  2618. map_and_fenceable,
  2619. nonblocking);
  2620. if (ret == 0)
  2621. goto search_free;
  2622. goto err_free_vma;
  2623. }
  2624. if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
  2625. obj->cache_level))) {
  2626. ret = -EINVAL;
  2627. goto err_remove_node;
  2628. }
  2629. ret = i915_gem_gtt_prepare_object(obj);
  2630. if (ret)
  2631. goto err_remove_node;
  2632. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2633. list_add_tail(&obj->mm_list, &vm->inactive_list);
  2634. list_add(&vma->vma_link, &obj->vma_list);
  2635. fenceable =
  2636. i915_gem_obj_ggtt_size(obj) == fence_size &&
  2637. (i915_gem_obj_ggtt_offset(obj) & (fence_alignment - 1)) == 0;
  2638. mappable = i915_gem_obj_ggtt_offset(obj) + obj->base.size <=
  2639. dev_priv->gtt.mappable_end;
  2640. obj->map_and_fenceable = mappable && fenceable;
  2641. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2642. i915_gem_verify_gtt(dev);
  2643. return 0;
  2644. err_remove_node:
  2645. drm_mm_remove_node(&vma->node);
  2646. err_free_vma:
  2647. i915_gem_vma_destroy(vma);
  2648. err_unpin:
  2649. i915_gem_object_unpin_pages(obj);
  2650. return ret;
  2651. }
  2652. void
  2653. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2654. {
  2655. /* If we don't have a page list set up, then we're not pinned
  2656. * to GPU, and we can ignore the cache flush because it'll happen
  2657. * again at bind time.
  2658. */
  2659. if (obj->pages == NULL)
  2660. return;
  2661. /*
  2662. * Stolen memory is always coherent with the GPU as it is explicitly
  2663. * marked as wc by the system, or the system is cache-coherent.
  2664. */
  2665. if (obj->stolen)
  2666. return;
  2667. /* If the GPU is snooping the contents of the CPU cache,
  2668. * we do not need to manually clear the CPU cache lines. However,
  2669. * the caches are only snooped when the render cache is
  2670. * flushed/invalidated. As we always have to emit invalidations
  2671. * and flushes when moving into and out of the RENDER domain, correct
  2672. * snooping behaviour occurs naturally as the result of our domain
  2673. * tracking.
  2674. */
  2675. if (obj->cache_level != I915_CACHE_NONE)
  2676. return;
  2677. trace_i915_gem_object_clflush(obj);
  2678. drm_clflush_sg(obj->pages);
  2679. }
  2680. /** Flushes the GTT write domain for the object if it's dirty. */
  2681. static void
  2682. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2683. {
  2684. uint32_t old_write_domain;
  2685. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2686. return;
  2687. /* No actual flushing is required for the GTT write domain. Writes
  2688. * to it immediately go to main memory as far as we know, so there's
  2689. * no chipset flush. It also doesn't land in render cache.
  2690. *
  2691. * However, we do have to enforce the order so that all writes through
  2692. * the GTT land before any writes to the device, such as updates to
  2693. * the GATT itself.
  2694. */
  2695. wmb();
  2696. old_write_domain = obj->base.write_domain;
  2697. obj->base.write_domain = 0;
  2698. trace_i915_gem_object_change_domain(obj,
  2699. obj->base.read_domains,
  2700. old_write_domain);
  2701. }
  2702. /** Flushes the CPU write domain for the object if it's dirty. */
  2703. static void
  2704. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2705. {
  2706. uint32_t old_write_domain;
  2707. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2708. return;
  2709. i915_gem_clflush_object(obj);
  2710. i915_gem_chipset_flush(obj->base.dev);
  2711. old_write_domain = obj->base.write_domain;
  2712. obj->base.write_domain = 0;
  2713. trace_i915_gem_object_change_domain(obj,
  2714. obj->base.read_domains,
  2715. old_write_domain);
  2716. }
  2717. /**
  2718. * Moves a single object to the GTT read, and possibly write domain.
  2719. *
  2720. * This function returns when the move is complete, including waiting on
  2721. * flushes to occur.
  2722. */
  2723. int
  2724. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2725. {
  2726. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2727. uint32_t old_write_domain, old_read_domains;
  2728. int ret;
  2729. /* Not valid to be called on unbound objects. */
  2730. if (!i915_gem_obj_ggtt_bound(obj))
  2731. return -EINVAL;
  2732. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2733. return 0;
  2734. ret = i915_gem_object_wait_rendering(obj, !write);
  2735. if (ret)
  2736. return ret;
  2737. i915_gem_object_flush_cpu_write_domain(obj);
  2738. /* Serialise direct access to this object with the barriers for
  2739. * coherent writes from the GPU, by effectively invalidating the
  2740. * GTT domain upon first access.
  2741. */
  2742. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2743. mb();
  2744. old_write_domain = obj->base.write_domain;
  2745. old_read_domains = obj->base.read_domains;
  2746. /* It should now be out of any other write domains, and we can update
  2747. * the domain values for our changes.
  2748. */
  2749. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2750. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2751. if (write) {
  2752. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2753. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2754. obj->dirty = 1;
  2755. }
  2756. trace_i915_gem_object_change_domain(obj,
  2757. old_read_domains,
  2758. old_write_domain);
  2759. /* And bump the LRU for this access */
  2760. if (i915_gem_object_is_inactive(obj))
  2761. list_move_tail(&obj->mm_list,
  2762. &dev_priv->gtt.base.inactive_list);
  2763. return 0;
  2764. }
  2765. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2766. enum i915_cache_level cache_level)
  2767. {
  2768. struct drm_device *dev = obj->base.dev;
  2769. drm_i915_private_t *dev_priv = dev->dev_private;
  2770. struct i915_vma *vma = __i915_gem_obj_to_vma(obj);
  2771. int ret;
  2772. if (obj->cache_level == cache_level)
  2773. return 0;
  2774. if (obj->pin_count) {
  2775. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2776. return -EBUSY;
  2777. }
  2778. if (vma && !i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
  2779. ret = i915_gem_object_unbind(obj);
  2780. if (ret)
  2781. return ret;
  2782. }
  2783. if (i915_gem_obj_ggtt_bound(obj)) {
  2784. ret = i915_gem_object_finish_gpu(obj);
  2785. if (ret)
  2786. return ret;
  2787. i915_gem_object_finish_gtt(obj);
  2788. /* Before SandyBridge, you could not use tiling or fence
  2789. * registers with snooped memory, so relinquish any fences
  2790. * currently pointing to our region in the aperture.
  2791. */
  2792. if (INTEL_INFO(dev)->gen < 6) {
  2793. ret = i915_gem_object_put_fence(obj);
  2794. if (ret)
  2795. return ret;
  2796. }
  2797. if (obj->has_global_gtt_mapping)
  2798. i915_gem_gtt_bind_object(obj, cache_level);
  2799. if (obj->has_aliasing_ppgtt_mapping)
  2800. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2801. obj, cache_level);
  2802. i915_gem_obj_ggtt_set_color(obj, cache_level);
  2803. }
  2804. if (cache_level == I915_CACHE_NONE) {
  2805. u32 old_read_domains, old_write_domain;
  2806. /* If we're coming from LLC cached, then we haven't
  2807. * actually been tracking whether the data is in the
  2808. * CPU cache or not, since we only allow one bit set
  2809. * in obj->write_domain and have been skipping the clflushes.
  2810. * Just set it to the CPU cache for now.
  2811. */
  2812. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2813. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2814. old_read_domains = obj->base.read_domains;
  2815. old_write_domain = obj->base.write_domain;
  2816. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2817. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2818. trace_i915_gem_object_change_domain(obj,
  2819. old_read_domains,
  2820. old_write_domain);
  2821. }
  2822. obj->cache_level = cache_level;
  2823. i915_gem_verify_gtt(dev);
  2824. return 0;
  2825. }
  2826. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2827. struct drm_file *file)
  2828. {
  2829. struct drm_i915_gem_caching *args = data;
  2830. struct drm_i915_gem_object *obj;
  2831. int ret;
  2832. ret = i915_mutex_lock_interruptible(dev);
  2833. if (ret)
  2834. return ret;
  2835. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2836. if (&obj->base == NULL) {
  2837. ret = -ENOENT;
  2838. goto unlock;
  2839. }
  2840. args->caching = obj->cache_level != I915_CACHE_NONE;
  2841. drm_gem_object_unreference(&obj->base);
  2842. unlock:
  2843. mutex_unlock(&dev->struct_mutex);
  2844. return ret;
  2845. }
  2846. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2847. struct drm_file *file)
  2848. {
  2849. struct drm_i915_gem_caching *args = data;
  2850. struct drm_i915_gem_object *obj;
  2851. enum i915_cache_level level;
  2852. int ret;
  2853. switch (args->caching) {
  2854. case I915_CACHING_NONE:
  2855. level = I915_CACHE_NONE;
  2856. break;
  2857. case I915_CACHING_CACHED:
  2858. level = I915_CACHE_LLC;
  2859. break;
  2860. default:
  2861. return -EINVAL;
  2862. }
  2863. ret = i915_mutex_lock_interruptible(dev);
  2864. if (ret)
  2865. return ret;
  2866. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2867. if (&obj->base == NULL) {
  2868. ret = -ENOENT;
  2869. goto unlock;
  2870. }
  2871. ret = i915_gem_object_set_cache_level(obj, level);
  2872. drm_gem_object_unreference(&obj->base);
  2873. unlock:
  2874. mutex_unlock(&dev->struct_mutex);
  2875. return ret;
  2876. }
  2877. /*
  2878. * Prepare buffer for display plane (scanout, cursors, etc).
  2879. * Can be called from an uninterruptible phase (modesetting) and allows
  2880. * any flushes to be pipelined (for pageflips).
  2881. */
  2882. int
  2883. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2884. u32 alignment,
  2885. struct intel_ring_buffer *pipelined)
  2886. {
  2887. u32 old_read_domains, old_write_domain;
  2888. int ret;
  2889. if (pipelined != obj->ring) {
  2890. ret = i915_gem_object_sync(obj, pipelined);
  2891. if (ret)
  2892. return ret;
  2893. }
  2894. /* The display engine is not coherent with the LLC cache on gen6. As
  2895. * a result, we make sure that the pinning that is about to occur is
  2896. * done with uncached PTEs. This is lowest common denominator for all
  2897. * chipsets.
  2898. *
  2899. * However for gen6+, we could do better by using the GFDT bit instead
  2900. * of uncaching, which would allow us to flush all the LLC-cached data
  2901. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2902. */
  2903. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2904. if (ret)
  2905. return ret;
  2906. /* As the user may map the buffer once pinned in the display plane
  2907. * (e.g. libkms for the bootup splash), we have to ensure that we
  2908. * always use map_and_fenceable for all scanout buffers.
  2909. */
  2910. ret = i915_gem_object_pin(obj, alignment, true, false);
  2911. if (ret)
  2912. return ret;
  2913. i915_gem_object_flush_cpu_write_domain(obj);
  2914. old_write_domain = obj->base.write_domain;
  2915. old_read_domains = obj->base.read_domains;
  2916. /* It should now be out of any other write domains, and we can update
  2917. * the domain values for our changes.
  2918. */
  2919. obj->base.write_domain = 0;
  2920. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2921. trace_i915_gem_object_change_domain(obj,
  2922. old_read_domains,
  2923. old_write_domain);
  2924. return 0;
  2925. }
  2926. int
  2927. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2928. {
  2929. int ret;
  2930. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2931. return 0;
  2932. ret = i915_gem_object_wait_rendering(obj, false);
  2933. if (ret)
  2934. return ret;
  2935. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2936. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2937. return 0;
  2938. }
  2939. /**
  2940. * Moves a single object to the CPU read, and possibly write domain.
  2941. *
  2942. * This function returns when the move is complete, including waiting on
  2943. * flushes to occur.
  2944. */
  2945. int
  2946. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2947. {
  2948. uint32_t old_write_domain, old_read_domains;
  2949. int ret;
  2950. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2951. return 0;
  2952. ret = i915_gem_object_wait_rendering(obj, !write);
  2953. if (ret)
  2954. return ret;
  2955. i915_gem_object_flush_gtt_write_domain(obj);
  2956. old_write_domain = obj->base.write_domain;
  2957. old_read_domains = obj->base.read_domains;
  2958. /* Flush the CPU cache if it's still invalid. */
  2959. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2960. i915_gem_clflush_object(obj);
  2961. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2962. }
  2963. /* It should now be out of any other write domains, and we can update
  2964. * the domain values for our changes.
  2965. */
  2966. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2967. /* If we're writing through the CPU, then the GPU read domains will
  2968. * need to be invalidated at next use.
  2969. */
  2970. if (write) {
  2971. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2972. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2973. }
  2974. trace_i915_gem_object_change_domain(obj,
  2975. old_read_domains,
  2976. old_write_domain);
  2977. return 0;
  2978. }
  2979. /* Throttle our rendering by waiting until the ring has completed our requests
  2980. * emitted over 20 msec ago.
  2981. *
  2982. * Note that if we were to use the current jiffies each time around the loop,
  2983. * we wouldn't escape the function with any frames outstanding if the time to
  2984. * render a frame was over 20ms.
  2985. *
  2986. * This should get us reasonable parallelism between CPU and GPU but also
  2987. * relatively low latency when blocking on a particular request to finish.
  2988. */
  2989. static int
  2990. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2991. {
  2992. struct drm_i915_private *dev_priv = dev->dev_private;
  2993. struct drm_i915_file_private *file_priv = file->driver_priv;
  2994. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2995. struct drm_i915_gem_request *request;
  2996. struct intel_ring_buffer *ring = NULL;
  2997. unsigned reset_counter;
  2998. u32 seqno = 0;
  2999. int ret;
  3000. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3001. if (ret)
  3002. return ret;
  3003. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3004. if (ret)
  3005. return ret;
  3006. spin_lock(&file_priv->mm.lock);
  3007. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3008. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3009. break;
  3010. ring = request->ring;
  3011. seqno = request->seqno;
  3012. }
  3013. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3014. spin_unlock(&file_priv->mm.lock);
  3015. if (seqno == 0)
  3016. return 0;
  3017. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  3018. if (ret == 0)
  3019. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3020. return ret;
  3021. }
  3022. int
  3023. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3024. uint32_t alignment,
  3025. bool map_and_fenceable,
  3026. bool nonblocking)
  3027. {
  3028. int ret;
  3029. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3030. return -EBUSY;
  3031. if (i915_gem_obj_ggtt_bound(obj)) {
  3032. if ((alignment && i915_gem_obj_ggtt_offset(obj) & (alignment - 1)) ||
  3033. (map_and_fenceable && !obj->map_and_fenceable)) {
  3034. WARN(obj->pin_count,
  3035. "bo is already pinned with incorrect alignment:"
  3036. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3037. " obj->map_and_fenceable=%d\n",
  3038. i915_gem_obj_ggtt_offset(obj), alignment,
  3039. map_and_fenceable,
  3040. obj->map_and_fenceable);
  3041. ret = i915_gem_object_unbind(obj);
  3042. if (ret)
  3043. return ret;
  3044. }
  3045. }
  3046. if (!i915_gem_obj_ggtt_bound(obj)) {
  3047. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3048. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  3049. map_and_fenceable,
  3050. nonblocking);
  3051. if (ret)
  3052. return ret;
  3053. if (!dev_priv->mm.aliasing_ppgtt)
  3054. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3055. }
  3056. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  3057. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3058. obj->pin_count++;
  3059. obj->pin_mappable |= map_and_fenceable;
  3060. return 0;
  3061. }
  3062. void
  3063. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  3064. {
  3065. BUG_ON(obj->pin_count == 0);
  3066. BUG_ON(!i915_gem_obj_ggtt_bound(obj));
  3067. if (--obj->pin_count == 0)
  3068. obj->pin_mappable = false;
  3069. }
  3070. int
  3071. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3072. struct drm_file *file)
  3073. {
  3074. struct drm_i915_gem_pin *args = data;
  3075. struct drm_i915_gem_object *obj;
  3076. int ret;
  3077. ret = i915_mutex_lock_interruptible(dev);
  3078. if (ret)
  3079. return ret;
  3080. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3081. if (&obj->base == NULL) {
  3082. ret = -ENOENT;
  3083. goto unlock;
  3084. }
  3085. if (obj->madv != I915_MADV_WILLNEED) {
  3086. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3087. ret = -EINVAL;
  3088. goto out;
  3089. }
  3090. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3091. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3092. args->handle);
  3093. ret = -EINVAL;
  3094. goto out;
  3095. }
  3096. if (obj->user_pin_count == 0) {
  3097. ret = i915_gem_object_pin(obj, args->alignment, true, false);
  3098. if (ret)
  3099. goto out;
  3100. }
  3101. obj->user_pin_count++;
  3102. obj->pin_filp = file;
  3103. /* XXX - flush the CPU caches for pinned objects
  3104. * as the X server doesn't manage domains yet
  3105. */
  3106. i915_gem_object_flush_cpu_write_domain(obj);
  3107. args->offset = i915_gem_obj_ggtt_offset(obj);
  3108. out:
  3109. drm_gem_object_unreference(&obj->base);
  3110. unlock:
  3111. mutex_unlock(&dev->struct_mutex);
  3112. return ret;
  3113. }
  3114. int
  3115. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3116. struct drm_file *file)
  3117. {
  3118. struct drm_i915_gem_pin *args = data;
  3119. struct drm_i915_gem_object *obj;
  3120. int ret;
  3121. ret = i915_mutex_lock_interruptible(dev);
  3122. if (ret)
  3123. return ret;
  3124. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3125. if (&obj->base == NULL) {
  3126. ret = -ENOENT;
  3127. goto unlock;
  3128. }
  3129. if (obj->pin_filp != file) {
  3130. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3131. args->handle);
  3132. ret = -EINVAL;
  3133. goto out;
  3134. }
  3135. obj->user_pin_count--;
  3136. if (obj->user_pin_count == 0) {
  3137. obj->pin_filp = NULL;
  3138. i915_gem_object_unpin(obj);
  3139. }
  3140. out:
  3141. drm_gem_object_unreference(&obj->base);
  3142. unlock:
  3143. mutex_unlock(&dev->struct_mutex);
  3144. return ret;
  3145. }
  3146. int
  3147. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3148. struct drm_file *file)
  3149. {
  3150. struct drm_i915_gem_busy *args = data;
  3151. struct drm_i915_gem_object *obj;
  3152. int ret;
  3153. ret = i915_mutex_lock_interruptible(dev);
  3154. if (ret)
  3155. return ret;
  3156. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3157. if (&obj->base == NULL) {
  3158. ret = -ENOENT;
  3159. goto unlock;
  3160. }
  3161. /* Count all active objects as busy, even if they are currently not used
  3162. * by the gpu. Users of this interface expect objects to eventually
  3163. * become non-busy without any further actions, therefore emit any
  3164. * necessary flushes here.
  3165. */
  3166. ret = i915_gem_object_flush_active(obj);
  3167. args->busy = obj->active;
  3168. if (obj->ring) {
  3169. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3170. args->busy |= intel_ring_flag(obj->ring) << 16;
  3171. }
  3172. drm_gem_object_unreference(&obj->base);
  3173. unlock:
  3174. mutex_unlock(&dev->struct_mutex);
  3175. return ret;
  3176. }
  3177. int
  3178. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3179. struct drm_file *file_priv)
  3180. {
  3181. return i915_gem_ring_throttle(dev, file_priv);
  3182. }
  3183. int
  3184. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3185. struct drm_file *file_priv)
  3186. {
  3187. struct drm_i915_gem_madvise *args = data;
  3188. struct drm_i915_gem_object *obj;
  3189. int ret;
  3190. switch (args->madv) {
  3191. case I915_MADV_DONTNEED:
  3192. case I915_MADV_WILLNEED:
  3193. break;
  3194. default:
  3195. return -EINVAL;
  3196. }
  3197. ret = i915_mutex_lock_interruptible(dev);
  3198. if (ret)
  3199. return ret;
  3200. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3201. if (&obj->base == NULL) {
  3202. ret = -ENOENT;
  3203. goto unlock;
  3204. }
  3205. if (obj->pin_count) {
  3206. ret = -EINVAL;
  3207. goto out;
  3208. }
  3209. if (obj->madv != __I915_MADV_PURGED)
  3210. obj->madv = args->madv;
  3211. /* if the object is no longer attached, discard its backing storage */
  3212. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3213. i915_gem_object_truncate(obj);
  3214. args->retained = obj->madv != __I915_MADV_PURGED;
  3215. out:
  3216. drm_gem_object_unreference(&obj->base);
  3217. unlock:
  3218. mutex_unlock(&dev->struct_mutex);
  3219. return ret;
  3220. }
  3221. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3222. const struct drm_i915_gem_object_ops *ops)
  3223. {
  3224. INIT_LIST_HEAD(&obj->mm_list);
  3225. INIT_LIST_HEAD(&obj->global_list);
  3226. INIT_LIST_HEAD(&obj->ring_list);
  3227. INIT_LIST_HEAD(&obj->exec_list);
  3228. INIT_LIST_HEAD(&obj->vma_list);
  3229. obj->ops = ops;
  3230. obj->fence_reg = I915_FENCE_REG_NONE;
  3231. obj->madv = I915_MADV_WILLNEED;
  3232. /* Avoid an unnecessary call to unbind on the first bind. */
  3233. obj->map_and_fenceable = true;
  3234. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3235. }
  3236. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3237. .get_pages = i915_gem_object_get_pages_gtt,
  3238. .put_pages = i915_gem_object_put_pages_gtt,
  3239. };
  3240. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3241. size_t size)
  3242. {
  3243. struct drm_i915_gem_object *obj;
  3244. struct address_space *mapping;
  3245. gfp_t mask;
  3246. obj = i915_gem_object_alloc(dev);
  3247. if (obj == NULL)
  3248. return NULL;
  3249. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3250. i915_gem_object_free(obj);
  3251. return NULL;
  3252. }
  3253. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3254. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3255. /* 965gm cannot relocate objects above 4GiB. */
  3256. mask &= ~__GFP_HIGHMEM;
  3257. mask |= __GFP_DMA32;
  3258. }
  3259. mapping = file_inode(obj->base.filp)->i_mapping;
  3260. mapping_set_gfp_mask(mapping, mask);
  3261. i915_gem_object_init(obj, &i915_gem_object_ops);
  3262. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3263. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3264. if (HAS_LLC(dev)) {
  3265. /* On some devices, we can have the GPU use the LLC (the CPU
  3266. * cache) for about a 10% performance improvement
  3267. * compared to uncached. Graphics requests other than
  3268. * display scanout are coherent with the CPU in
  3269. * accessing this cache. This means in this mode we
  3270. * don't need to clflush on the CPU side, and on the
  3271. * GPU side we only need to flush internal caches to
  3272. * get data visible to the CPU.
  3273. *
  3274. * However, we maintain the display planes as UC, and so
  3275. * need to rebind when first used as such.
  3276. */
  3277. obj->cache_level = I915_CACHE_LLC;
  3278. } else
  3279. obj->cache_level = I915_CACHE_NONE;
  3280. return obj;
  3281. }
  3282. int i915_gem_init_object(struct drm_gem_object *obj)
  3283. {
  3284. BUG();
  3285. return 0;
  3286. }
  3287. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3288. {
  3289. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3290. struct drm_device *dev = obj->base.dev;
  3291. drm_i915_private_t *dev_priv = dev->dev_private;
  3292. trace_i915_gem_object_destroy(obj);
  3293. if (obj->phys_obj)
  3294. i915_gem_detach_phys_object(dev, obj);
  3295. obj->pin_count = 0;
  3296. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  3297. bool was_interruptible;
  3298. was_interruptible = dev_priv->mm.interruptible;
  3299. dev_priv->mm.interruptible = false;
  3300. WARN_ON(i915_gem_object_unbind(obj));
  3301. dev_priv->mm.interruptible = was_interruptible;
  3302. }
  3303. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3304. * before progressing. */
  3305. if (obj->stolen)
  3306. i915_gem_object_unpin_pages(obj);
  3307. if (WARN_ON(obj->pages_pin_count))
  3308. obj->pages_pin_count = 0;
  3309. i915_gem_object_put_pages(obj);
  3310. i915_gem_object_free_mmap_offset(obj);
  3311. i915_gem_object_release_stolen(obj);
  3312. BUG_ON(obj->pages);
  3313. if (obj->base.import_attach)
  3314. drm_prime_gem_destroy(&obj->base, NULL);
  3315. drm_gem_object_release(&obj->base);
  3316. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3317. kfree(obj->bit_17);
  3318. i915_gem_object_free(obj);
  3319. }
  3320. struct i915_vma *i915_gem_vma_create(struct drm_i915_gem_object *obj,
  3321. struct i915_address_space *vm)
  3322. {
  3323. struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  3324. if (vma == NULL)
  3325. return ERR_PTR(-ENOMEM);
  3326. INIT_LIST_HEAD(&vma->vma_link);
  3327. vma->vm = vm;
  3328. vma->obj = obj;
  3329. return vma;
  3330. }
  3331. void i915_gem_vma_destroy(struct i915_vma *vma)
  3332. {
  3333. WARN_ON(vma->node.allocated);
  3334. kfree(vma);
  3335. }
  3336. int
  3337. i915_gem_idle(struct drm_device *dev)
  3338. {
  3339. drm_i915_private_t *dev_priv = dev->dev_private;
  3340. int ret;
  3341. if (dev_priv->ums.mm_suspended) {
  3342. mutex_unlock(&dev->struct_mutex);
  3343. return 0;
  3344. }
  3345. ret = i915_gpu_idle(dev);
  3346. if (ret) {
  3347. mutex_unlock(&dev->struct_mutex);
  3348. return ret;
  3349. }
  3350. i915_gem_retire_requests(dev);
  3351. /* Under UMS, be paranoid and evict. */
  3352. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3353. i915_gem_evict_everything(dev);
  3354. i915_gem_reset_fences(dev);
  3355. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3356. i915_kernel_lost_context(dev);
  3357. i915_gem_cleanup_ringbuffer(dev);
  3358. /* Cancel the retire work handler, which should be idle now. */
  3359. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3360. return 0;
  3361. }
  3362. void i915_gem_l3_remap(struct drm_device *dev)
  3363. {
  3364. drm_i915_private_t *dev_priv = dev->dev_private;
  3365. u32 misccpctl;
  3366. int i;
  3367. if (!HAS_L3_GPU_CACHE(dev))
  3368. return;
  3369. if (!dev_priv->l3_parity.remap_info)
  3370. return;
  3371. misccpctl = I915_READ(GEN7_MISCCPCTL);
  3372. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  3373. POSTING_READ(GEN7_MISCCPCTL);
  3374. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3375. u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
  3376. if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
  3377. DRM_DEBUG("0x%x was already programmed to %x\n",
  3378. GEN7_L3LOG_BASE + i, remap);
  3379. if (remap && !dev_priv->l3_parity.remap_info[i/4])
  3380. DRM_DEBUG_DRIVER("Clearing remapped register\n");
  3381. I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
  3382. }
  3383. /* Make sure all the writes land before disabling dop clock gating */
  3384. POSTING_READ(GEN7_L3LOG_BASE);
  3385. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  3386. }
  3387. void i915_gem_init_swizzling(struct drm_device *dev)
  3388. {
  3389. drm_i915_private_t *dev_priv = dev->dev_private;
  3390. if (INTEL_INFO(dev)->gen < 5 ||
  3391. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3392. return;
  3393. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3394. DISP_TILE_SURFACE_SWIZZLING);
  3395. if (IS_GEN5(dev))
  3396. return;
  3397. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3398. if (IS_GEN6(dev))
  3399. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3400. else if (IS_GEN7(dev))
  3401. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3402. else
  3403. BUG();
  3404. }
  3405. static bool
  3406. intel_enable_blt(struct drm_device *dev)
  3407. {
  3408. if (!HAS_BLT(dev))
  3409. return false;
  3410. /* The blitter was dysfunctional on early prototypes */
  3411. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3412. DRM_INFO("BLT not supported on this pre-production hardware;"
  3413. " graphics performance will be degraded.\n");
  3414. return false;
  3415. }
  3416. return true;
  3417. }
  3418. static int i915_gem_init_rings(struct drm_device *dev)
  3419. {
  3420. struct drm_i915_private *dev_priv = dev->dev_private;
  3421. int ret;
  3422. ret = intel_init_render_ring_buffer(dev);
  3423. if (ret)
  3424. return ret;
  3425. if (HAS_BSD(dev)) {
  3426. ret = intel_init_bsd_ring_buffer(dev);
  3427. if (ret)
  3428. goto cleanup_render_ring;
  3429. }
  3430. if (intel_enable_blt(dev)) {
  3431. ret = intel_init_blt_ring_buffer(dev);
  3432. if (ret)
  3433. goto cleanup_bsd_ring;
  3434. }
  3435. if (HAS_VEBOX(dev)) {
  3436. ret = intel_init_vebox_ring_buffer(dev);
  3437. if (ret)
  3438. goto cleanup_blt_ring;
  3439. }
  3440. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3441. if (ret)
  3442. goto cleanup_vebox_ring;
  3443. return 0;
  3444. cleanup_vebox_ring:
  3445. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3446. cleanup_blt_ring:
  3447. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3448. cleanup_bsd_ring:
  3449. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3450. cleanup_render_ring:
  3451. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3452. return ret;
  3453. }
  3454. int
  3455. i915_gem_init_hw(struct drm_device *dev)
  3456. {
  3457. drm_i915_private_t *dev_priv = dev->dev_private;
  3458. int ret;
  3459. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3460. return -EIO;
  3461. if (dev_priv->ellc_size)
  3462. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3463. if (HAS_PCH_NOP(dev)) {
  3464. u32 temp = I915_READ(GEN7_MSG_CTL);
  3465. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3466. I915_WRITE(GEN7_MSG_CTL, temp);
  3467. }
  3468. i915_gem_l3_remap(dev);
  3469. i915_gem_init_swizzling(dev);
  3470. ret = i915_gem_init_rings(dev);
  3471. if (ret)
  3472. return ret;
  3473. /*
  3474. * XXX: There was some w/a described somewhere suggesting loading
  3475. * contexts before PPGTT.
  3476. */
  3477. i915_gem_context_init(dev);
  3478. if (dev_priv->mm.aliasing_ppgtt) {
  3479. ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
  3480. if (ret) {
  3481. i915_gem_cleanup_aliasing_ppgtt(dev);
  3482. DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
  3483. }
  3484. }
  3485. return 0;
  3486. }
  3487. int i915_gem_init(struct drm_device *dev)
  3488. {
  3489. struct drm_i915_private *dev_priv = dev->dev_private;
  3490. int ret;
  3491. mutex_lock(&dev->struct_mutex);
  3492. if (IS_VALLEYVIEW(dev)) {
  3493. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3494. I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
  3495. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
  3496. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3497. }
  3498. i915_gem_init_global_gtt(dev);
  3499. ret = i915_gem_init_hw(dev);
  3500. mutex_unlock(&dev->struct_mutex);
  3501. if (ret) {
  3502. i915_gem_cleanup_aliasing_ppgtt(dev);
  3503. return ret;
  3504. }
  3505. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3506. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3507. dev_priv->dri1.allow_batchbuffer = 1;
  3508. return 0;
  3509. }
  3510. void
  3511. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3512. {
  3513. drm_i915_private_t *dev_priv = dev->dev_private;
  3514. struct intel_ring_buffer *ring;
  3515. int i;
  3516. for_each_ring(ring, dev_priv, i)
  3517. intel_cleanup_ring_buffer(ring);
  3518. }
  3519. int
  3520. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3521. struct drm_file *file_priv)
  3522. {
  3523. struct drm_i915_private *dev_priv = dev->dev_private;
  3524. int ret;
  3525. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3526. return 0;
  3527. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  3528. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3529. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  3530. }
  3531. mutex_lock(&dev->struct_mutex);
  3532. dev_priv->ums.mm_suspended = 0;
  3533. ret = i915_gem_init_hw(dev);
  3534. if (ret != 0) {
  3535. mutex_unlock(&dev->struct_mutex);
  3536. return ret;
  3537. }
  3538. BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
  3539. mutex_unlock(&dev->struct_mutex);
  3540. ret = drm_irq_install(dev);
  3541. if (ret)
  3542. goto cleanup_ringbuffer;
  3543. return 0;
  3544. cleanup_ringbuffer:
  3545. mutex_lock(&dev->struct_mutex);
  3546. i915_gem_cleanup_ringbuffer(dev);
  3547. dev_priv->ums.mm_suspended = 1;
  3548. mutex_unlock(&dev->struct_mutex);
  3549. return ret;
  3550. }
  3551. int
  3552. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3553. struct drm_file *file_priv)
  3554. {
  3555. struct drm_i915_private *dev_priv = dev->dev_private;
  3556. int ret;
  3557. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3558. return 0;
  3559. drm_irq_uninstall(dev);
  3560. mutex_lock(&dev->struct_mutex);
  3561. ret = i915_gem_idle(dev);
  3562. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3563. * We need to replace this with a semaphore, or something.
  3564. * And not confound ums.mm_suspended!
  3565. */
  3566. if (ret != 0)
  3567. dev_priv->ums.mm_suspended = 1;
  3568. mutex_unlock(&dev->struct_mutex);
  3569. return ret;
  3570. }
  3571. void
  3572. i915_gem_lastclose(struct drm_device *dev)
  3573. {
  3574. int ret;
  3575. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3576. return;
  3577. mutex_lock(&dev->struct_mutex);
  3578. ret = i915_gem_idle(dev);
  3579. if (ret)
  3580. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3581. mutex_unlock(&dev->struct_mutex);
  3582. }
  3583. static void
  3584. init_ring_lists(struct intel_ring_buffer *ring)
  3585. {
  3586. INIT_LIST_HEAD(&ring->active_list);
  3587. INIT_LIST_HEAD(&ring->request_list);
  3588. }
  3589. void
  3590. i915_gem_load(struct drm_device *dev)
  3591. {
  3592. drm_i915_private_t *dev_priv = dev->dev_private;
  3593. int i;
  3594. dev_priv->slab =
  3595. kmem_cache_create("i915_gem_object",
  3596. sizeof(struct drm_i915_gem_object), 0,
  3597. SLAB_HWCACHE_ALIGN,
  3598. NULL);
  3599. INIT_LIST_HEAD(&dev_priv->gtt.base.active_list);
  3600. INIT_LIST_HEAD(&dev_priv->gtt.base.inactive_list);
  3601. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3602. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3603. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3604. for (i = 0; i < I915_NUM_RINGS; i++)
  3605. init_ring_lists(&dev_priv->ring[i]);
  3606. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3607. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3608. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3609. i915_gem_retire_work_handler);
  3610. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3611. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3612. if (IS_GEN3(dev)) {
  3613. I915_WRITE(MI_ARB_STATE,
  3614. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3615. }
  3616. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3617. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3618. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3619. dev_priv->fence_reg_start = 3;
  3620. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  3621. dev_priv->num_fence_regs = 32;
  3622. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3623. dev_priv->num_fence_regs = 16;
  3624. else
  3625. dev_priv->num_fence_regs = 8;
  3626. /* Initialize fence registers to zero */
  3627. i915_gem_reset_fences(dev);
  3628. i915_gem_detect_bit_6_swizzle(dev);
  3629. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3630. dev_priv->mm.interruptible = true;
  3631. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3632. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3633. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3634. }
  3635. /*
  3636. * Create a physically contiguous memory object for this object
  3637. * e.g. for cursor + overlay regs
  3638. */
  3639. static int i915_gem_init_phys_object(struct drm_device *dev,
  3640. int id, int size, int align)
  3641. {
  3642. drm_i915_private_t *dev_priv = dev->dev_private;
  3643. struct drm_i915_gem_phys_object *phys_obj;
  3644. int ret;
  3645. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3646. return 0;
  3647. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3648. if (!phys_obj)
  3649. return -ENOMEM;
  3650. phys_obj->id = id;
  3651. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3652. if (!phys_obj->handle) {
  3653. ret = -ENOMEM;
  3654. goto kfree_obj;
  3655. }
  3656. #ifdef CONFIG_X86
  3657. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3658. #endif
  3659. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3660. return 0;
  3661. kfree_obj:
  3662. kfree(phys_obj);
  3663. return ret;
  3664. }
  3665. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3666. {
  3667. drm_i915_private_t *dev_priv = dev->dev_private;
  3668. struct drm_i915_gem_phys_object *phys_obj;
  3669. if (!dev_priv->mm.phys_objs[id - 1])
  3670. return;
  3671. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3672. if (phys_obj->cur_obj) {
  3673. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3674. }
  3675. #ifdef CONFIG_X86
  3676. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3677. #endif
  3678. drm_pci_free(dev, phys_obj->handle);
  3679. kfree(phys_obj);
  3680. dev_priv->mm.phys_objs[id - 1] = NULL;
  3681. }
  3682. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3683. {
  3684. int i;
  3685. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3686. i915_gem_free_phys_object(dev, i);
  3687. }
  3688. void i915_gem_detach_phys_object(struct drm_device *dev,
  3689. struct drm_i915_gem_object *obj)
  3690. {
  3691. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3692. char *vaddr;
  3693. int i;
  3694. int page_count;
  3695. if (!obj->phys_obj)
  3696. return;
  3697. vaddr = obj->phys_obj->handle->vaddr;
  3698. page_count = obj->base.size / PAGE_SIZE;
  3699. for (i = 0; i < page_count; i++) {
  3700. struct page *page = shmem_read_mapping_page(mapping, i);
  3701. if (!IS_ERR(page)) {
  3702. char *dst = kmap_atomic(page);
  3703. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3704. kunmap_atomic(dst);
  3705. drm_clflush_pages(&page, 1);
  3706. set_page_dirty(page);
  3707. mark_page_accessed(page);
  3708. page_cache_release(page);
  3709. }
  3710. }
  3711. i915_gem_chipset_flush(dev);
  3712. obj->phys_obj->cur_obj = NULL;
  3713. obj->phys_obj = NULL;
  3714. }
  3715. int
  3716. i915_gem_attach_phys_object(struct drm_device *dev,
  3717. struct drm_i915_gem_object *obj,
  3718. int id,
  3719. int align)
  3720. {
  3721. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3722. drm_i915_private_t *dev_priv = dev->dev_private;
  3723. int ret = 0;
  3724. int page_count;
  3725. int i;
  3726. if (id > I915_MAX_PHYS_OBJECT)
  3727. return -EINVAL;
  3728. if (obj->phys_obj) {
  3729. if (obj->phys_obj->id == id)
  3730. return 0;
  3731. i915_gem_detach_phys_object(dev, obj);
  3732. }
  3733. /* create a new object */
  3734. if (!dev_priv->mm.phys_objs[id - 1]) {
  3735. ret = i915_gem_init_phys_object(dev, id,
  3736. obj->base.size, align);
  3737. if (ret) {
  3738. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3739. id, obj->base.size);
  3740. return ret;
  3741. }
  3742. }
  3743. /* bind to the object */
  3744. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3745. obj->phys_obj->cur_obj = obj;
  3746. page_count = obj->base.size / PAGE_SIZE;
  3747. for (i = 0; i < page_count; i++) {
  3748. struct page *page;
  3749. char *dst, *src;
  3750. page = shmem_read_mapping_page(mapping, i);
  3751. if (IS_ERR(page))
  3752. return PTR_ERR(page);
  3753. src = kmap_atomic(page);
  3754. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3755. memcpy(dst, src, PAGE_SIZE);
  3756. kunmap_atomic(src);
  3757. mark_page_accessed(page);
  3758. page_cache_release(page);
  3759. }
  3760. return 0;
  3761. }
  3762. static int
  3763. i915_gem_phys_pwrite(struct drm_device *dev,
  3764. struct drm_i915_gem_object *obj,
  3765. struct drm_i915_gem_pwrite *args,
  3766. struct drm_file *file_priv)
  3767. {
  3768. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3769. char __user *user_data = to_user_ptr(args->data_ptr);
  3770. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3771. unsigned long unwritten;
  3772. /* The physical object once assigned is fixed for the lifetime
  3773. * of the obj, so we can safely drop the lock and continue
  3774. * to access vaddr.
  3775. */
  3776. mutex_unlock(&dev->struct_mutex);
  3777. unwritten = copy_from_user(vaddr, user_data, args->size);
  3778. mutex_lock(&dev->struct_mutex);
  3779. if (unwritten)
  3780. return -EFAULT;
  3781. }
  3782. i915_gem_chipset_flush(dev);
  3783. return 0;
  3784. }
  3785. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3786. {
  3787. struct drm_i915_file_private *file_priv = file->driver_priv;
  3788. /* Clean up our request list when the client is going away, so that
  3789. * later retire_requests won't dereference our soon-to-be-gone
  3790. * file_priv.
  3791. */
  3792. spin_lock(&file_priv->mm.lock);
  3793. while (!list_empty(&file_priv->mm.request_list)) {
  3794. struct drm_i915_gem_request *request;
  3795. request = list_first_entry(&file_priv->mm.request_list,
  3796. struct drm_i915_gem_request,
  3797. client_list);
  3798. list_del(&request->client_list);
  3799. request->file_priv = NULL;
  3800. }
  3801. spin_unlock(&file_priv->mm.lock);
  3802. }
  3803. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3804. {
  3805. if (!mutex_is_locked(mutex))
  3806. return false;
  3807. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3808. return mutex->owner == task;
  3809. #else
  3810. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3811. return false;
  3812. #endif
  3813. }
  3814. static int
  3815. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3816. {
  3817. struct drm_i915_private *dev_priv =
  3818. container_of(shrinker,
  3819. struct drm_i915_private,
  3820. mm.inactive_shrinker);
  3821. struct drm_device *dev = dev_priv->dev;
  3822. struct i915_address_space *vm = &dev_priv->gtt.base;
  3823. struct drm_i915_gem_object *obj;
  3824. int nr_to_scan = sc->nr_to_scan;
  3825. bool unlock = true;
  3826. int cnt;
  3827. if (!mutex_trylock(&dev->struct_mutex)) {
  3828. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  3829. return 0;
  3830. if (dev_priv->mm.shrinker_no_lock_stealing)
  3831. return 0;
  3832. unlock = false;
  3833. }
  3834. if (nr_to_scan) {
  3835. nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
  3836. if (nr_to_scan > 0)
  3837. nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
  3838. false);
  3839. if (nr_to_scan > 0)
  3840. i915_gem_shrink_all(dev_priv);
  3841. }
  3842. cnt = 0;
  3843. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  3844. if (obj->pages_pin_count == 0)
  3845. cnt += obj->base.size >> PAGE_SHIFT;
  3846. list_for_each_entry(obj, &vm->inactive_list, global_list)
  3847. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  3848. cnt += obj->base.size >> PAGE_SHIFT;
  3849. if (unlock)
  3850. mutex_unlock(&dev->struct_mutex);
  3851. return cnt;
  3852. }